Answers for "grep for lines before and after match"

1

how to grep lines before a pattern

cat abc.txt | grep -B 3 "your_pattern"  #  will grep 3 lines bofore your pattern..
cat abc.txt | grep -A 3 "your_pattern"  # will grep 3 lines after your pattern..
tac abc.txt | grep -A 3 "your_pattern" # will grep 3 lines bofore your pattern..
cat abc.txt | grep -B 3 -A 3 "your_pattern"  
tac is upside down of the file , cat - tac



$ cat testing
aaa
11
21
31
41
51
aaa
12
22
32
42
51
aaa
13
23
33
33
43
53
aaa

$ cat testing | grep -A 2 aaa
aaa
11
21
--
aaa
12
22
--
aaa
13
23
--
aaa

tac testing | grep -A 2 aaa
aaa
53
43
--
aaa
82
72
--
aaa
61
51
--
aaa
Posted by: Guest on January-31-2021

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language