Home > Software design >  How to see several lines under one grep line
How to see several lines under one grep line

Time:02-14

I want to see the logs where log level is ERROR and I want to see the stackTrace but when I input like below cat app.log | grep ERROR I can't see the stackTrace Is there a way to see several lines under the greped line?

CodePudding user response:

You can use -A NUM as part of the grep options.

For example: cat app.log | grep ERROR -A 10 will print 10 lines after the matching line(s).

From the grep man page:

-A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.

CodePudding user response:

grep -A 3 app.log
  • -A 3 ("after") prints matching line, and 3 more below (4 lines total)
  • -B 3 ("before") prints matching line, and 3 more before (4 lines total)
  • -C 3 ("context") prints matching line, and 3 above and 3 below (7 lines total)
  • Related