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

Printing previous lines !!

Status
Not open for further replies.

christiniaroelin

Programmer
Sep 15, 2004
26
US
Hi all,
I have the following code:
awk ' { str[NR] = $0 } END {
if ( ( $0 ~ / files found/) && ( $2< '$min_file'))
{
print $2 " files found : WARNING : EXPECTED MINIMUM NUMBER OF FILES " "'"$min_file"'" "\n\n"
print NR
for ( i=NR; i>(NR-5) ; i-- )
print str

}
if ( ( $0 ~ / *usr /) && ( $3 < 9)) {
print $0 "\n WARNING : HEADER NLOD FOUND FOR THE ABOVE LISTING HAVING ONLY : " $3 " BYTES \n\n"
}
}' < outnlod.dat

Im trying to print the previous 5 lines from -files found- record and which meets the criteria of min files found.It all worked fine until i added the for statement to print the previous 5 lines that meets the 'if' criteria.
Please let me know what im missing. I'm a beginner and learning awk by experimenting with examples as above. Your help in my awk pursuit is much appreciated.

Thanks again
Roelin
 
You may try this revision of your code:
awk '
{ str[NR] = $0 }
/ files found/{
if ( $2< '$min_file' )
{
print $2 " files found : WARNING : EXPECTED MINIMUM NUMBER OF FILES " "'"$min_file"'" "\n\n"
print NR
for ( i=NR; i>(NR-5) ; i-- )
print str
}
}
/ *usr /{ if ( $3 < 9))
print $0 "\n WARNING : HEADER NLOD FOUND FOR THE ABOVE LISTING HAVING ONLY : " $3 " BYTES \n\n"
}
' < outnlod.dat

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
christiniaroelin, if you want to learn the right way to write Awk programs:

Don't do this:
if ( ( $0 ~ / files found/) && ( $2< '$min_file'))

Instead, make a file containing only the Awk program and run it this way:
[tt]
awk -v min_file="$min_file" -f prog.awk infile >outfile
[/tt]
The line in your program should be changed to
if ( ( $0 ~ / files found/) && ( $2 < min_file))
 
Thanks a lot PHV and futurlet..!!
Appreciate your time and help!!

Regrads,
Roelin!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top