Saturday 13 January 2018

Using grep to show lines adjacent to the search string

Using a file with the following contents:
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10

cat test_data | grep -B2 'line 5'
will display:
line 3
line 4
line 5


cat test_data | grep -A2 'line 5'
will display:
line 5
line 6
line 7


cat test_data | grep -A2 -B2 'line 5'
will display:
line 3
line 4
line 5
line 6
line 7


No comments:

Post a Comment