Subject: one-liner smart Perl command instead of a simple grep (in Bash script)
I have a problem when I use grep to match the unusual characters (I can\'t put the \"\\\"
I'm not sure I understand your question, exactly. Perhaps -P
and -x
do what you want, along with quoting "$Some_string"
to preserve whitespace?
grep -Px "$Some_string" file
From the grep man page:
-P
,--perl-regexp
Interpret PATTERN as a Perl regular expression.
-x
,--line-regexp
Select only those matches that exactly match the whole line.
You could do something like this:
perl -ne 'print if m|\Q[ * ( & % \E\@\Q !]\E|' file
I've found the \Q and \E operator in Quote and Quote-like Operators.
A snippet from that link:
You cannot include a literal $ or @ within a \Q sequence. An unescaped $ or @ interpolates the corresponding variable, while escaping will cause the literal string \$ to be inserted. You'll need to write something like m/\Quser\E@\Qhost/.
\Q Quote (disable) pattern metacharacters till \E or end of string \E End either case modification or quoted section (whichever was last seen)
You will have to escape the string somehow. Using \
to escape special characters is required in this case, because passing the string to Perl is really no different than passing it to grep -- both will see the string after the shell processes it.
Using Perl to do this will simply obfuscate your code.