How to grep the IP address from ifconfig output

$ ip -o addr show | awk '/inet/ {print $2, $3, $4}'
lo inet 127.0.0.1/8
lo inet6 ::1/128
eth0 inet 192.168.0.1/24
eth0 inet6 fe80::2a0:feed:dead:beef/64

Aw man I can't resist trying to improve my perl oneliner scripting ability when I see a post like this. Here's a first hacky attempt:

$ /sbin/ifconfig | \
perl -ane "(\$n)=(/^(\S+)/) and print \"\$n: \"; print (/addr:(\S+)/) and print \"\n\" if /inet/"
eth0: 192.168.0.11
lo: 127.0.0.1

That could be tightened up a lot, and it has lots of problem like it doesn't handle ipv6 addresses, etc.


This is how I do it:

ETHERADD=$(/sbin/ifconfig |grep -e ^[a-z] |grep -v lo | awk '{ printf $1 FS}')

for i in $ETHERADD  
do
    IP=$(/sbin/ifconfig $i | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' | head -1)
    printf "%-16s%-42s*\n" "IP $i:"      "$IP"
done