views:

54

answers:

4

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 all lines containing "Error" ignoring the case sensitivity so it can be error, Error, ERROR etc.

+1  A: 

you can use

grep -i error report.txt
動靜能量
+1  A: 

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' !!

codaddict
A: 

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
grantc
this is a so called useless use of cat: grep -i error report.txt | more does the same :-)
WMR
+2  A: 

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.

WMR