Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looking for a single line command to search for line above and print line + 1

Status
Not open for further replies.

smik

Technical User
Apr 26, 2016
2
AU

Hi

looking for a single line command to search for line and print line immediately above
plus the second line, third line and fourth line above

awk '/search_for_string/{x=$0;next}{print x";"$0;}'

Thanks in advance

Mik
 
Hi

From your code I deduce that you want each set of matches on one line, separated with semicolon, including the matched line itself.
Code:
awk '/search_for_string/{for(i=0;i<4;i++)printf"%s;",a[(i+NR)%4];print}{a[NR%4]=$0}'


Feherke.
feherke.ga
 
Hi Feheke, Thank you for the one liner much appreciated :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top