Bootstrap FreeKB - Bash (Scripting) - grep previous or next match
Bash (Scripting) - grep previous or next match

Updated:   |  Bash (Scripting) articles

Let's consider a file with these lines.

line 1 before
line 2 before
HELLO WORLD
line 1 after
line 2 after

 


Line(s) before

-B1 means "the match and one line Before".  

grep -B1 "HELLO WORLD" example.txt

 

-B1 will yeild the following result.

line 2 before
HELLO WORLD

 

-B2 means "the match and two lines Before".  

grep -B2 "HELLO WORLD" example.txt

 

-B2 will yeild the following result.

line 1 before
line 2 before
HELLO WORLD

 


Line(s) after

-A1 means "the match and one line After".

grep -A1 "HELLO WORLD" example.txt

 

-A1 will yield the following result.

HELLO WORLD
line 1 after

 

-A2 means "the match and two lines After".

grep -A2 "HELLO WORLD" example.txt

 

-A2 will yield the following result.

HELLO WORLD
line 1 after
line 2 after



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 441563 in the box below so that we can be sure you are a human.