Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This is a snapshot of error log:

06:16:29,933 ERROR EmailRMManager$:45 - Exception In get Message
com.rabbitmq.client.AlreadyClosedException: clean connection shutdown; reason: Attempt to use closed channel
    at com.rabbitmq.client.impl.AMQChannel.ensureIsOpen(AMQChannel.java:195)
    at com.rabbitmq.client.impl.AMQChannel.rpc(AMQChannel.java:222)
    at com.rabbitmq.client.impl.AMQChannel.rpc(AMQChannel.java:208)
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:139)
    at com.rabbitmq.client.impl.ChannelN.basicGet(ChannelN.java:645)

I do the following command:

cat foo.log | grep ERROR to get an OP as:

06:16:29,933 ERROR EmailRMManager$:45 - Exception In get Message

What command should I execute to get the output as

06:16:29,933 ERROR EmailRMManager$:45 - Exception In get Message
    com.rabbitmq.client.AlreadyClosedException: clean connection shutdown; reason: Attempt to use closed channel

ie, also grep the line(s) after the pattern?

share|improve this question
add comment

2 Answers

up vote 29 down vote accepted

Just do a:

grep -A1 ERROR

The -A1 tells grep to include 1 line after the match. -B includes lines before the match, in case you need that too.

share|improve this answer
    
oh right, that would be helpful too. –  theTuxRacer Feb 24 '11 at 6:34
5  
And -C includes lines both before and after the match (the 'C' stands for 'context', I believe). –  Marius Gedminas Feb 24 '11 at 16:01
add comment

For a more portable way, there's awk

awk '/ERROR/{n=NR+1} n>=NR' foo.log

Or maybe you want all the indented lines following?

awk '/^[^[:blank:]]/{p=0} /ERROR/{p=1} p' foo.log
share|improve this answer
    
:O that is good information, but rather overkill! nonetheless, its good to know a different method :) –  theTuxRacer Feb 25 '11 at 4:55
    
I wish I understood how those awk commands work. –  Firefeather Mar 4 '11 at 20:49
3  
@Firefeather awk.freeshell.org is a good resource for learning awk. The GNU awk manual page is pretty good too. –  geirha Mar 5 '11 at 12:28
    
Thanks, I'll check those out. –  Firefeather Mar 9 '11 at 20:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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