Trying to pass regular expression to grep
I'm trying to exctract error lines from my log file:
I used this :
more report.txt | grep -E (?i)(error)
I'm getting this error msg :
bash: syntax error near unexpected token `('
What am I doing wrong? I'm trying to extract al开发者_高级运维l lines containing "Error" ignoring the case sensitivity so it can be error, Error, ERROR etc.
The problem with your line is that the parens are picked up by the shell instead of grep, you need to quote them:
grep -E '(?i)(error)' report.txt
For this particular task the other answers are of course correct, you don't even need the parens.
You can do:
grep -i error report.txt
There is really no need to more
the file and then pipe it to grep
. You can pass the filename as an argument to grep
.
To make the search case insensitive, you use the -i
option of grep
.
And there is really no need to go for -E
option as the pattern you are using error
is not an extended regex.
The cause of the error you are seeing is that your pattern (?i)(error)
is interpreted by the shell and since the shell did not except to see (
in this context it throws an error, something similar to when you do ls (*)
.
To fix this you quote your pattern. But that will not solve your problem of finding error
in the file because the pattern (?i)(error)
looks for the string 'ierror'
!!
you can use
grep -i error report.txt
this will achieve the same result
cat report.txt | grep -i error
and if you want to paginate the results:
cat report.txt | grep -i error | more
精彩评论