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!

Conditional awk question

Status
Not open for further replies.

cstorm

MIS
Oct 1, 2001
69
US
Is there a way using awk to print text if a certain condition is met? For example, print the Hostname line and subsequent lines until you see the next line beginning with Hostname. Do not print the Hostname line if the next line begins with Hostname or a blank line. Output file contains:

Hostname: server1.am.acme1.com
account1 Jane Doe
account2 Joe Smith

Hostname: server2.am.acme1.com
Hostname: server3.am.acme1.com

Hostname: server4.am.acme1.com
account1 Jalen Tulip
account2 LeBron Jim

Hostname: server5.am.acme1.com

Hostname: server6.am.acme1.com

Hostname: server7.am.acme1.com




Desired output:

Hostname: server1.am.acme1.com
account1 Jane Doe
account2 Joe Smith

Hostname: server4.am.acme1.com
account1 Jalen Tulip
account2 LeBron Jim


Thanks,
CSTORM








 
Try something like this:
Code:
awk '
/Hostname/{host=$0;i=0;next}
NF{if(!i)print host;print;++i;next}
i{print "";i=0}
' /path/to/input

Hope This Help
PH.
 
PVH,

Your code worked perfectly. I appreciate your help.

CSTORM
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top