56

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?

1
  • Is com.rabbitmq.client text on next line starts from the begginning or have some spaces in front of it? Commented Nov 11, 2016 at 9:09

4 Answers 4

97

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.

3
  • oh right, that would be helpful too. Commented Feb 24, 2011 at 6:34
  • 14
    And -C includes lines both before and after the match (the 'C' stands for 'context', I believe). Commented Feb 24, 2011 at 16:01
  • I always get this mixed up because I instinctively think these stand for "above" and "below". Commented Jun 29, 2021 at 11:36
7

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
3
  • 2
    :O that is good information, but rather overkill! nonetheless, its good to know a different method :) Commented Feb 25, 2011 at 4:55
  • I wish I understood how those awk commands work. Commented Mar 4, 2011 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
    Commented Mar 5, 2011 at 12:28
3

I have found this solution:

cat apache.error.log | grep -Pzo '^.*?Exception In get Message.*?\ncom\.rabbitmq.*?(\n(?=\s).*?)*$'

Where (\n(?=\s).*?)* means:

  • \n find next line
  • (?=\s) where is starts from whitespace character
  • .*? until end of line
  • (...)* Find such lines multiple times

PS. You may turf this pattern \ncom\.rabbitmq.*? if second line begins from whitespace \s

2

For this simple task a simple way is:

grep -A num
Print num lines of trailing context after each match. See also the -B and -C options.

grep -B num
Print num lines of leading context before each match. See also the -A and -C options.

grep -C num
Print num lines of leading and trailing context surrounding each match.
The default is 2 and is equivalent to -A 2 -B 2. 

Note: no whitespace may be given between the option and its argument.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .