FreeKB - grep next line or prior line in Linux Bash
grep next line or prior line in Linux Bash

Home > Search
  by

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 -B1 "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 example.txt

 

-A1 will yield the following result.

HELLO WORLD
line 1 after

 

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

grep -A1 example.txt

 

-A2 will yield the following result.

HELLO WORLD
line 1 after
line 2 after


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments