Originally Posted by
jcdenton1995
This actually works! it was the '$' that I was missing. I thought that wrapping a string in quotes was sufficient to protect the meta-characters in it from being expanded by the shell? why is the '$' necessary? (or does the '$' ensure that '\n' is expanded by the shell??)
Perhaps I missed it, but this is a bash-specific thing. Other shells would do it different. Bash expansion (regex/globbing) is mostly intuitive, until it isn't.
If it were me, I'd use egrep (the same as grep -E) which is for 'enhanced regex grep'. 2 fewer characters to type, 'eg{tab}' does the command completion fast.
Code:
-E, --extended-regexp
Interpret PATTERNS as extended regular expressions (EREs, see
below).
-P, --perl-regexp
Interpret PATTERNS as Perl-compatible regular expressions
(PCREs). This option is experimental when combined with the -z
(--null-data) option, and grep -P may warn of unimplemented
features.
This basically results in nearly python/perl regex handling, so there are fewer surprises. I know perl, so those regex are sometime needed, -P option, but that is seldom, since whenever a bash script gets over 1 page in length, I switch to perl for all the added capabilities like multi-line regex codes with comments. Perl hasn't been write-only in a very long time.
If multiple regex are needed, consider using multiple -e options, one for each.
Bookmarks