Skip to content

Instantly share code, notes, and snippets.

@multidis
Created November 30, 2013 23:48
Show Gist options
  • Save multidis/7726199 to your computer and use it in GitHub Desktop.
Save multidis/7726199 to your computer and use it in GitHub Desktop.
Regex-related commonly used tasks with shell scripting.
## 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