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!

AWK join on condition

Status
Not open for further replies.

cstorm

MIS
Oct 1, 2001
69
US
Gurus,

I need to join lines of a file based on a condition. If line 1 contains a line that ends with a ":" and the following line begins with the string "Average", then I want to join those 2 lines. If the line that ends with ":" is not followed by a line that begins with the string "Average", then I do not wnat to print that line.

My input file contains:

serverabc:
Average 5 4 3 87
hostdef:
Average 3 2 1 93
systemghi:
nodejkl:
Average 6 5 5 83

I need the output to be:

serverabc:Average 5 4 3 87
hostdef:Average 3 2 1 93
nodejkl:Average 6 5 5 83

Thanks,

CSTORM

 
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
 
This is what I have tried, but it does not work:

awk '
/^\*:$/{f=0}
!f && /^Average*$/{++f;x="";next}
f{x=x"\n"$0}
END{print substr(x,2)}
' filename
 
And what about this ?
awk '
/^Average/{if(x~/:$/)print x $0}
{x=$0}
' filename

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

Nice! It works great. I am having problems understanding exactly how the code "{if(x~/:$/)" works.

Thanks for you help!
CSTORM
 
It evaluates to true of the string in variable x matches the regex ":$", i.e. if it ends in a ":".

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top