I am looking for a string within quotation marks where the word Linux occurs somewhere between parentheses which reside somewhere inside the quotation marks. I have to use a regular expression to do that. I am required to do it in a Linux OS (using Virtual Box Terminal). I used the following regex:
egrep "\"([^"]*)?\(([^"]*)?Linux([^"]*)?\)([^"]*)?\"" [FILE]
But the problem is, I get
bash: syntax error near unexpected token '("
every time. What can be the problem? I tried this regex in "http://regexr.com/" it works fine, there is nothing wrong with the regex. But the Linux shell always produces the same problem… I used "\" in front of parentheses because they are metacharacters. The same thing I did in front of double quotes… But it does not work. Could anybody help me, please?
Best Answer
The problem is the unescaped double quotes inside the expression, for example around this part
"]*)?\(([^]"
. If you want to pass literal"
, including inside double quotes or()
or[]
, then you have to quote the quotes! You could doBut a less ugly way is to quote the whole expression with single quotes, and in general always strong (single) quote your regex so the shell does not do anything weird to it before it's passed as an argument to
egrep
or whatever program you're using:(now you do not need to escape the double quotes on the outside either, but you need the other backslashes to make the parentheses literal (in ERE))
if you don't want the rest of the line, add
-o
(match only) alsoYou didn't give an example so I made a silly one... here is is before:
and after:
(NB: using GNU
grep
you can dogrep -E
instead of usingegrep
)