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!

Formatting output between patterns

Status
Not open for further replies.

cstorm

MIS
Oct 1, 2001
69
US
Is there a way for awk to format data that falls between a certain pattern? For example, I have a file in the following format and I want to print the line containing the Hostname and fields 1 and 5 of the lines that follow.

File looks like:

Hostname is: server1.d34.acme.com
wb01308:x:102:101:John Smith:/home/wb01308:/usr/bin/ksh
u3x2010:x:23620:9249:Jim Davis:/home/u3x2010:/usr/bin/ksh
$ --------------------------------------------------------
Hostname is: server2.d54.acme.com
u3x5163:x:191:102:Jane Doe:/home/u3x5163:/usr/bin/ksh
u3x5063:x:194:102:Austin Powers:/home/u3x5063:/usr/bin/ksh
$ ---------------------------------------------------------
Hostname is: server3.d45.acme.com
u3x2010:x:23620:9249:Jim Davis:/home/u3x2010:/usr/bin/ksh
$ ---------------------------------------------------------
Hostname is: engora2.d52.acme.com
u3x2010:x:23620:9249:Jim Davis:/home/u3x2010:/usr/bin/ksh
$ ---------------------------------------------------------
Hostname is: server4.d25.acme.com
u3x2010:x:23620:9249:Jim Davis:/home/u3x2010:/usr/bin/ksh
$ ---------------------------------------------------------


I need the output to look like:

Hostname is: server1.d34.acme.com
wb01308 John Smith
u3x2010 Jim Davis

Hostname is: server2.d54.acme.com
u3x5163 Jane Doe
u3x5063 Austin Powers

Hostname is: server3.d45.acme.com
u3x2010 Jim Davis

Hostname is: engora2.d52.acme.com
u3x2010 Jim Davis

Hostname is: server4.d25.acme.com
u3x2010 Jim Davis


I know I need to use: awk -F: '{print $1, $5}' filename
I have a question on including the line containing Hostname.

Thanks,

CSTORM
 
nawk -f cstorm.awk myFile.txt

#---------------- cstorm.awk
BEGIN {
FS=":"
OFS=" ";
}

/^Hostname/ { print; next}
/^\$/ { print "";next}
{ print $1, $5;}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Whoa. That worked perfectly.

Thank you very much!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top