1

I want to filter grep results by using grep -v option. But the output does not differ when using a particular pattern. contents of log.log:

ERROR
error
EXCEPTION
exception
<STATUS>ERROR</STATUS>
<MessageType>ERROR</MessageType>

When I run the command:

egrep -wi 'error|exception' /temp/log.log | grep -v 'error'

gives output:

ERROR
EXCEPTION
exception
<STATUS>ERROR</STATUS>
<MessageType>ERROR</MessageType>

But if I run the command:

egrep -wi 'error|exception' /temp/log.log | grep -vi '<STATUS>ERROR</STATUS>'

It still gives the output as:

ERROR
EXCEPTION
exception
<STATUS>ERROR</STATUS>
<MessageType>ERROR</MessageType>

Whereas I am expecting:

ERROR
EXCEPTION
exception
<MessageType>ERROR</MessageType>

Why is this happening?

  • 3
    When I run the command it works. Please look for typos. – hellcode Oct 29 '15 at 10:30
  • 1
    Given that the command works for me, you should make a bug report against your implementation of "grep". – schily Oct 29 '15 at 10:42
  • 1
    What operating system are you on? Is your grep aliased to anything? What's the output of type grep? – terdon Oct 29 '15 at 12:55
  • 3
    Any chance you have got non-printable characters in your '<STATUS>ERROR</STATUS>' line? – Harald Oct 29 '15 at 22:02
0

The problem occurs due to the egrep -w 'error|exception'. This command adds special characters before and after the pattern (ie. error or exception) for highlighting these patterns in the grep result.

It is as Harald mentioned. The 'ERROR' pattern did not match grep output statement as the 'ERROR' between the STATUS tags in grep output had some special characters added to it by the -w flag.

Hence when the egrep's result was sent to grep -v command, it failed to match the pattern.

  • 1
    No, it doesn't add special characters to color the output if the output is not a terminal: egrep -wi 'error|exception' /temp/log.log | hexdump -c. You probably have a bugged version of grep (or, more likely, as Harald mentioned, strange characters in the <STATUS>ERROR</STATUS> line). Please add the output of hexdump -c /temp/log.log to your question – kos Oct 30 '15 at 5:22
  • oh I did not know that, but my std ouput is a terminal – AB DC Oct 30 '15 at 5:29
  • 1
    The standard output is normally the terminal, but if you redirect the output to another command the standard output becomes the anonymous pipe generated by | to pass the output to the second command. Try egrep -wi 'error|exception' /temp/log.log | hexdump -c, hexdump -c will show the output as read by grep in the misbehaving command. There are no color escapes in the hexdump -c output. I suspect your file has some strange characters, so check the output of hexdump -c /temp/log.log (or maybe add it to the question). Otherwise, you found a bug in your grep version. – kos Oct 30 '15 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.