-   Programming (
-   -   Using grep and awk to get IP address (

snowman81 03-12-2008 06:47 PM

Using grep and awk to get IP address
I need to print out the ip address and send it to an admin. I used this command

ifconfig | grep 'inet addr'
but it gave a few too many adreeses (the broadcast and things like that).

So I tried this one

ifconfig | grep addr | awk '{print $1}'
and it gave me this


What would be the command to just get the ip address?

Edit: I almost got it by changing the one to a two but I get this


dxqcanada 03-12-2008 06:54 PM

Your grep "inet addr" is OK.

You could [print $2} but that would also include the word "addr:"


snowman81 03-12-2008 06:56 PM

Yeah, I don't think I'll get closer than that, but I think I can make it work. Thanks.

chrism01 03-12-2008 07:38 PM

This works for me:

/sbin/ifconfig|grep 'inet addr'|cut -d':' -f2|awk '{print $1}'

ghostdog74 03-12-2008 08:27 PM


# ifconfig | awk -F ":"  '/inet addr/{split($2,a," ");print a[1]}'

jschiwal 03-12-2008 09:09 PM

If you wanted to isolate the section for one device, this form of sed is very common:
/sbin/ifconfig | sed -n '/^eth2/,/^$/p'

This form works for the output of a whole group of commands that print a blank line between records of output, such as lspci for example.

For your particular task, you can put the device to report after the ifconfig command:
/sbin/ifconfig eth2 | sed -n '/inet addr:/s/ *inet addr:\([[:digit:].]*\) .*/\1/p'

snowman81 03-12-2008 09:16 PM

The last one just gives me my IP address and not the loopback, so that's perfect. Thanks.

P.S, is there any way to make that into a variable? Like

ifconfig eth0 | sed -n '/inet addr:/s/ *inet addr:\([[:digit:].]*\) .*/\1/p' > var1

jschiwal 03-12-2008 09:30 PM

Just a few thoughts based on the title of this thread.

You don't need to use both grep and sed.
A sed command that begins with slashes in the address field (before the command) will only operate on lines where that pattern is found. So "sed -n '/<pattern>/p' file" is the same as "grep '<pattern>' file". You can also use two patterns to define a range of lines to work on: "sed -n '/<pattern1>/,/<pattern2>/p'.

Including N lines after the match is easy as well:

/sbin/ifconfig | sed -n '/eth2/,+1p'
eth2      Link encap:Ethernet  HWaddr 00:04:5A:9D:D6:E8
          inet addr:  Bcast:  Mask:

One case where you might precede sed with grep is if you want to include N lines preceding a match:
grep -B5 '<pattern>' file | sed ...

I was surprised when I looked at the relative sizes of the sed, grep and perl programs.
The sed program is smaller than grep! That was a surprise to me. Perl is a lot larger than sed.

Hko 03-13-2008 05:59 AM

I use this:

ip -f inet addr show dev eth0 | sed -n 's/^ *inet *\([.0-9]*\).*/\1/p'
It outputs clean IP-address.

chrism01 03-13-2008 11:45 PM

Given that loopback should always be

var=$(/sbin/ifconfig|grep 'inet addr'|cut -d':' -f2|awk '{print $1}'|grep -v

to put it into the variable

angrybanana 03-14-2008 02:17 AM

Eh.. just to be different :p

var=$(expr "`ifconfig eth0`" : '.*inet addr:\([^ ]*\).*')
But since you asked for awk/grep, here's yet another awk solution

ifconfig eth0| awk -F ' *|:' '/inet addr/{print $4}'

konsolebox 03-14-2008 02:54 AM

using only sed:

ifconfig | sed -n /'inet addr'/s'/.*addr:\([0-9.]\+\).*/\1/p'
var=$(ifconfig | sed -n /'inet addr'/s'/.*addr:\([0-9.]\+\).*/\1/p')

sorry i haven't seen jschiwal's code.. mine is just a dup of his

rocketero 08-25-2012 10:45 AM


Originally Posted by Hko (Post 3087203)
I use this:

ip -f inet addr show dev eth0 | sed -n 's/^ *inet *\([.0-9]*\).*/\1/p'
It outputs clean IP-address.

so far yours is the best solution.
to use it in a variable i used:

echo $(ip -f inet addr show dev eth0 | sed -n 's/^ *inet *\([.0-9]*\).*/\1/p')
var=$(ip -f inet addr show dev eth0 | sed -n 's/^ *inet *\([.0-9]*\).*/\1/p')

All times are GMT -5. The time now is 07:01 PM.