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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for a string & find a string 3 rows above it. 1

Status
Not open for further replies.

stu78

Programmer
May 29, 2002
121
GB
I have the following log file.

line 1: uk-test
line 2: Description
line 3: 1.1.1.1
line 4: test
line 5: true

I need to find if the line ( line 5 ) exists and if so print this and line 2 to a log file. If not skip. The problem is that this situation occurs 3 lines above it. This is the piece that does not work.

Is this possible.
 
Something like this ?
awk '
{a[NR%4]=$0}
/pattern identifying line 5/{print a[(NR-3)%4],$0}
' /path/to/input >>/path/to/logfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks - works great.

How can I output this into a comma delimited file?
 
Try to replace this:
print a[(NR-3)%4],$0
By this:
printf "%s,%s\n",a[(NR-3)%4],$0

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top