grep show lines before and after

Usually when searching for keywords, we need to look at the keyword context information, all of which use -A, -B and -C parameters.

Show num rows after matching – grep -A

➜  grep -A 3 "\-A num" man-grep.txt

Show num rows before matching – grep -B

➜  grep -B 3 "\-B num" man-grep.txt

Show num lines before and after matching – grep -C

➜  grep -C 3 "\-C\[num" man-grep.txt
Equivalent to : grep -A 3 -B 3 
➜  grep -A 3 -B 3 "\-C\[num" man-grep.txt

When keywords are matched, sometimes matching rows need to be located, using the – n parameter.

grep show line number – grep -n

➜  grep -C 3 -n "\-line\-number" man-grep.txt

Add a Comment

Your email address will not be published.