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!

search pattern and get line 46 before its occurance

Status
Not open for further replies.

schocku

Programmer
Nov 20, 2001
23
US
Hi all.
I have a fixed format log file with content as follows :
========
06/18/2003 12:00:00:008 blah blah
06/18/2003 12:00:02:010 blah blah
..
..
..
06/18/2003 12:00:00:016 blah blah
06/18/2003 12:00:03:020 submission 124304
..
..
06/18/2003 12:00:04:200 blah blah
06/18/2003 12:00:05:020 submission 124304
=============

Can you please help me with a script which searches for string "124304" in the file and extracts the 46th line before EACH occurance of the string in the file.

Thanks for your help in advance.
 

Try this

/124304/ {
print a[ix]
}
{
a[ix] = $0
ix++
if (ix>n) ix=1
}

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
CaKiwi thanks for your response. The script worked perfectly. An additional question. Instead of printing the 46th line before the pattern, can I search for the word "Case:" before the pattern 124304 and print that line.

So if the file is as below
========
06/18/2003 12:00:00:008 blah blah
06/18/2003 12:00:02:010 Case: 111
..
..
..
06/18/2003 12:00:02:016 blah blah
06/18/2003 12:00:03:020 submission 124304
..
06/18/2003 12:00:03:190 Case: 222
..
..
06/18/2003 12:00:04:200 blah blah
06/18/2003 12:00:05:020 submission 124304
=============
the output should be

06/18/2003 12:00:02:010 Case: 111
06/18/2003 12:00:03:190 Case: 222

Thanks again for your help.
 

/Case:/{a=$0}
/124304/{print a}

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
CaKiwi,
My problem is solved. Thanks for your prompt response.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top