Please give your inputs !!!!

I am trying to extract two fields from the output of ifconfig command on one of my sun server . The output looks like :
root@e08k18:/tmp/test# ifconfig -a
lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1
inet 127.0.0.1 netmask ff000000
ce0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
inet 10.177.4.61 netmask ffffff00 broadcast 10.177.4.255
groupname ipmp0
ether 0:3:ba:6c:7f:2e

I require output as field1 filed2 for all the interfaces ...but when I try
la0 127.0.01
ce0 10.177.4.61

# ifconfig |awk -F ":" {print $1 $3)'

However I am not getting the required output.
Any clue in this regard will be of great help.
Thanking you in advance.

The output of ifconfig are in different lines. You will have to join the lines and then maybe use awk. An example would be like this:

-------------
#!/bin/sh

ifconfig -a | awk -F":" '{print $1}' |
awk 'BEGIN {x = 0}
{
if (x<2) {
printf("%s ",$0)
x=x+1
}
if (x==2) {
printf("\n")
x = 0
}
}' | awk '{print $1, $3}'
-------------

Thanks for your reply I have tried this :-

ifconfig -a | awk -F":" '{print $1}' |
awk 'BEGIN {x = 0}
{
if (x<2) {
printf("%s ",$0)
x=x+1
}
if (x==2) {
printf("\n")
x = 0
}
}' | awk '{print $1, $3}'

and it gives the output as :-
ce2 172.16.0.129
ce6 172.16.1.1
clprivnet0 172.16.193.1
ce0 10.177.4.61
ce0:1 10.177.4.70
ce0:2 10.177.4.66
ce0:3 10.177.4.67
ce0:4 10.177.4.65
ce4 10.177.4.62
ce3 10.177.224.80
ce0 10.177.4.61

Thanks once again !!!!!

Please use a title that describes your problem.

ifconfig |
 awk '
  $1 ~ /:$/ { sub( /:$/, "", $1)
                    ifname = $1 }
  $1 == "inet" { print ifname, $2 }
' 

ifconfig | awk ' /flags/ { x = $1 ; next } /inet/ { print x $2 }' :slight_smile:

A shorter solution is also this:

ifconfig -a | awk -F":" '{print $1}' | paste - - | awk '{print $1, $3}'

Shorter (but not by much), slower (2 unnecessary external commands), and incorrect for the sample given:

lo0 127.0.0.1
ce0 10.177.4.61
groupname ether

True its not the fastest solution but ifconfig wouldn't have 1000s of lines of output so I would say it won't hurt but thanks for the point.

The difference will be more noticeable with a small amount of data, since the external commands will take a much greater percentage of the total time.

The extra time taken is not much on its own, but snippets of code are often incorporated into larger scripts. The inefficiencies of such code will be multiplied when there are a number of them in a script, especially if they are in a loop.

The more important issue in this case is that the output of the command is incorrect.

Looks correct on my system. But based on his "ifconfig", you are right.