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!

Printing the last record in AWK 1

Status
Not open for further replies.

madasafish

Technical User
Jul 18, 2006
78
TH
Given the following data, I want to use nawk to print the last record (records being of variable length between "2006" eg: RS="2006" that has [Warning] in the record. Ideally, without the [Warning] line. The record I am interesting in getting is shown in red. I would appreciate any help with this.

Thanks in advance
Madasafish

==========================================================
2006/ 5/31/(Wed.)14: 3:26 [Warning] It passed from start more than 35 minutes, But the following shape is not found.

HT-2875 68260 JD000-B1

ASM : 68260_JD000-B1

HT-2875 68260 JD410-B1

ASM : 68260_JD410-B1

HT-2875 68260 JD41E-B1

ASM : 68260_JD41E-B1

2006/ 5/31/(Wed.)14: 8:26 [Warning] It passed from start more than 40 minutes, But the following shape is not found.

HT-2875 68260 JD000-B1

ASM : 68260_JD000-B1

HT-2875 68260 JD410-B1

ASM : 68260_JD410-B1

HT-2875 68260 JD41E-B1

ASM : 68260_JD41E-B1




2006/ 5/31/(Wed.)14:12:49 Release-Info. File = E:\input\HT-2875.rlse

2006/ 5/31/(Wed.)14:12:49 HT-2875 Search start. Number of model-data: 8 (All 85 line ) [Q:\output]

2006/ 5/31/(Wed.)14:12:49 Found 28038_JD410-B1 in Q:\output\HT-2875\28038_JD410-B1_002\28038.txt 1/8

2006/ 5/31/(Wed.)14:12:49 Found 28038_JD42A-B1 in Q:\output\HT-2875\28038_JD42A-B1_002\28038.txt 2/8

2006/ 5/31/(Wed.)14:12:49 Found 28039_JD41A-B1 in Q:\output\HT-2875\28039_JD41A-B1_002\28039.txt 3/8

2006/ 5/31/(Wed.)14:12:49 Found 28039_JD42A-B1 in Q:\output\HT-2875\28039_JD42A-B1_002\28039.txt 4/8

2006/ 5/31/(Wed.)14:12:50 Found 68260_JD400-B1 in Q:\output\HT-2875\68260_JD400-B1_003\68260.txt 5/8
 
And what have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
A starting point:
nawk '
/^2006/{f=0}
!f && /\[Warning]/{++f;x="";next}
f{x=x"\n"$0}
END{print substr(x,2)}
' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Super Wammy!.

Your a Star!

I look forward to testing the consistency of this on other files at work tomorrow.

I much appreciate your rapid response to a long time problem.

madasafish
 
Hello PHV,

I am trying to read it and understand what you did in the nawk script. I can see you search for 2006 and the rest is well....lost. It would be great if you could explain.

Will that script find the LAST record with [Warning] in it?

Best Regards,

Madasafish


 
Will that script find the LAST record with [Warning] in it?
Not exactly, it will PRINT the LAST record FOUND

if you could explain
/^2006/{f=0} # if a line starts with "2006" set a flag to 0
!f && /\[Warning]/{++f;x="";next} # if the flag=0 and the line contains "[Warning]" set the flag to 1, empty a spare buffer and stop current line's processing
f{x=x"\n"$0} # if the flag!=0 append a LineFeed and the current line to the spare buffer
END{print substr(x,2)} # at end of the file print the spare buffer but the first LineFeed

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

Part and Inventory Search

Sponsor

Back
Top