· df · 1 min read

Print next line after a pattern

print next lineone linersawkTechnology

Just had a requirement to print the current line with pattern and next line.

This seems to be pretty straight forward with AWK

awk ‘/YOUR_STRING/{c=1;{print}next}c–>0’   <yourFile>

if you put c=2, it will print the next two lines

 

Eg.  sampleData.txt containg..

EmployeeName
ABC
XYZ
QPR
LMN
awk ‘/EmployeeName/{c=1;{print}next}c–>0’   sampleData.txt

will print..

EmployeeName
ABC

if you don’t want headers..
awk ‘/EmployeeName/{c=1;next}c–>0’   sampleData.txt