How can I make grep print the lines below and above each matching line? [duplicate] How can I make grep print the lines below and above each matching line? [duplicate] linux linux

How can I make grep print the lines below and above each matching line? [duplicate]


grep's -A 1 option will give you one line after; -B 1 will give you one line before; and -C 1 combines both to give you one line both before and after, -1 does the same.


Use -B, -A or -C option

grep --help...-B, --before-context=NUM  print NUM lines of leading context-A, --after-context=NUM   print NUM lines of trailing context-C, --context=NUM         print NUM lines of output context-NUM                      same as --context=NUM...


Use -A and -B switches (mean lines-after and lines-before):

grep -A 1 -B 1 FAILED file.txt