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

NAWK script to copy the lines before a particular word

Status
Not open for further replies.

sethu

Programmer
Apr 15, 2000
29
US
Hi AWK gurus,

I need to write to a file from a log file after the script finds a particular word. The file which I read is a log file.

Let me give an example. The three lines are like this

02 20 2001
There are errors in file.
Test was failure.

We will serach for a word Test and I need to print out all the three lines (ie the line with the word Test and two lines before that. How can I achieve this?

--Sethu


 
Hi sethu-

Here is one way:

nawk '{

while (NR % 3 != 0) {
lines[++i] = $0
next
}

if ((NR % 3 == 0) && ($0 ~ /Test/)) {
print lines [i-1]
print lines
print $0
print ""
for (i in lines)
delete lines
next
}

}' infile > outfile

Hope this helps you!


flogrr
flogr@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top