Grep Lines Before and After

I often need to grep a file and return a few lines before and a few lines after the line I'm looking for. There are three relevant command line Arguments that allow you to do that.

Grep's -B 1 option will give you one line before; -A 1 will give you one line after; and -C 1 will give you both one line before and one line after.

Here's an example.

grep -B 2 -A 3 'captcha' ./myfile.txt
comments powered by Disqus
linux/grep_lines_before_and_after.txt · Last modified: 2020/06/01 22:53 (external edit)