Created
November 30, 2013 23:48
-
-
Save multidis/7726199 to your computer and use it in GitHub Desktop.
Regex-related commonly used tasks with shell scripting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## extract part of a string with grep | |
echo "12 BBQ ,45 rofl, 89 lol"|grep -P '\d+ (?=rofl)' -o | |
## -P means Perl-style regex, -o means match only not the full line | |
## extract part of a line from every line in a file $FILE | |
grep -P 'home.*' -o "$FILE" | while read -r line ; do | |
echo "$line" | |
## do something with extracted string | |
## e.g. capture part of it into another variable | |
VARNAME=$(echo "$line"|grep -P "$DIR.*" -o) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment