rusa23
rusa23

Reputation: 1

Print line after a specific contained word line in UNIX

I have problem, I want to parse a log file, I want to print a line if the above line is containing a specific word,

for example

line 1 containing : aaa

line 2 containing : bbb

so, it will print out bbb

Upvotes: 0

Views: 451

Answers (2)

Aryeh Leib Taurog
Aryeh Leib Taurog

Reputation: 5598

If you don't want the 'aaa' printed as well, you could use sed(1):

sed -n '/aaa/{n;p}'

Explanation:

-n    don't print every line
/aaa/ when this pattern is matched, execute the block that follows
n     advance to the next line
p     print what's in the buffer

Upvotes: 0

Barmar
Barmar

Reputation: 780851

The -A n option to grep prints the next n lines after the matching line.

grep -A 1 aaa logfile

Upvotes: 1

Related Questions