Answers for "grep lines with n characters"

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
0

how to print next lines with grep

grep -A 2 "searchText" mytext.txt ## next 2 lines after match line
grep -B 2 "seacrchText" mytext.txt ## next 2 lines before match line
grep -C 2 "searchText" mytext.txt ## next 2 lines before and after match line
Posted by: Guest on October-03-2021

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language