Last Updated: February 25, 2016
·
710
· milesmatthias

Print Surrounding Lines Around a Match

By default, the unix tool grep will only print the line that your keyword is found on. You can expand this behavior with the -A or -C flag.

grep -A 3 bob file.txt

prints the 3 lines after the keyword 'bob' is found in 'file.txt'

grep -C 3 bob file.txt

prints the 3 lines before and after the keyword 'bob' is found in 'file.txt'

In both cases, the line where 'bob' appears will still be printed.