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!

Adding a header 3

Status
Not open for further replies.

kHz

MIS
Dec 6, 2004
1,359
US
I have a file that contains ifconfig data for all my servers and lists the hostname before the ifconfig.

For example:
==== Host1 ====
lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1
inet 127.0.0.1 netmask ff000000
bge0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
inet xx.xx.xxx.x netmask ffffffc0 broadcast xx.xx.xxx.xx
bge1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
inet xxx.xx.xxx.xx netmask ffffff00 broadcast xxx.xx.xxx.xxx
bge2: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 4
inet xxx.xx.xx.xx netmask ff000000 broadcast xxx.xxx.xxx.xxx
bge3: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 5
inet xx.xx.xxx.xxx netmask ffffffc0 broadcast xx.xx.xxx.xxx

==== Host2 ====
lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1
inet 127.0.0.1 netmask ff000000
bge0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
inet xx.xx.xxx.xx netmask ffffffc0 broadcast xx.xx.xxx.xx
bge1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
inet xxx.xx.xxx.xx netmask ffffff00 broadcast xxx.xx.xxx.xxx
bge2: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 4
inet xxx.xx.xx.xx netmask ffff0000 broadcast xxx.xx.xxx.xxx
bge3: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 5
inet xx.xx.xxx.xxx netmask ffffffe0 broadcast xx.xx.xxx.xxx

What I want is:
Host1
lo0: 127.0.0.1
bge0: xx.xx.xxx.xxx
bge1: xx.xx.xxx.xxx
bge2: xx.xx.xxx.xxx
bge3: xx.xx.xxx.xxx

Host2
bge0: xx.xx.xxx.xxx
bge1: xx.xx.xxx.xxx
bge2: xx.xx.xxx.xxx
bge3: xx.xx.xxx.xxx

I can cat the file and use:
cat s1 | awk '$1~/:$/{f=$1;getline;print f,$2}'
to display:
bge0: xx.xx.xxx.xxx
bge1: xx.xx.xxx.xxx
bge2: xx.xx.xxx.xxx
bge3: xx.xx.xxx.xxx
bge0: xx.xx.xxx.xxx
bge1: xx.xx.xxx.xxx
bge2: xx.xx.xxx.xxx
bge3: xx.xx.xxx.xxx

But I need the hostname to separate the interfaces as I described above.

Thanks!
 
Something like this ?
awk '
/^===/{print $2}
$1~/:$/{f=$1;getline;print f,$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 FAQ222-2244
 
Code:
/^=/ { print $2; next }
$1 ~ /:$/ { printf "%s ", $1; next }
{ print $2 }

Let me know whether or not this helps.

If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. For an introduction to Awk, see faq271-5564.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top