grep show lines before and after match code example
Example 1: grep show lines above and below
grep -C 3 foo README.txt
Example 2: grep line after match
Include -A flag to add lines after match and -B flag to add lines before
grep match -A 3 #display match line and 3 after lines
grep match -B 2 #display match line and 2 lines before
Example 3: grep show lines between matches
Just use following structure to get result between two different patterns:
sed -n '/^pattern1/,/^pattern2/p;/^pattern2/q' file.txt