LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2008, 07:47 PM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 281

Rep: Reputation: 30
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
Code:
ifconfig | grep 'inet addr'
but it gave a few too many adreeses (the broadcast and things like that).

So I tried this one
Code:
ifconfig | grep addr | awk '{print $1}'
and it gave me this
Code:
eth0
inet
inet6
inet
inet6
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
Code:
addr:68.*.*.*
addr:
addr:127.0.0.1
addr:

Last edited by snowman81; 03-12-2008 at 07:49 PM.
 
Old 03-12-2008, 07:54 PM   #2
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
Your grep "inet addr" is OK.

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

...
 
Old 03-12-2008, 07:56 PM   #3
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 281

Original Poster
Rep: Reputation: 30
Yeah, I don't think I'll get closer than that, but I think I can make it work. Thanks.
 
Old 03-12-2008, 08:38 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,828

Rep: Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559
This works for me:

/sbin/ifconfig|grep 'inet addr'|cut -d':' -f2|awk '{print $1}'
 
Old 03-12-2008, 09:27 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
# ifconfig | awk -F ":"  '/inet addr/{split($2,a," ");print a[1]}'
127.0.0.1
 
Old 03-12-2008, 10:09 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680
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'
 
Old 03-12-2008, 10:16 PM   #7
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 281

Original Poster
Rep: Reputation: 30
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
Code:
 ifconfig eth0 | sed -n '/inet addr:/s/ *inet addr:\([[:digit:].]*\) .*/\1/p' > var1

Last edited by snowman81; 03-12-2008 at 10:23 PM.
 
Old 03-12-2008, 10:30 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680
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:
Code:
/sbin/ifconfig | sed -n '/eth2/,+1p'
eth2      Link encap:Ethernet  HWaddr 00:04:5A:9D:D6:E8
          inet addr:192.168.1.102  Bcast:192.168.1.127  Mask:255.255.255.128
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.
 
Old 03-13-2008, 06:59 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I use this:
Code:
ip -f inet addr show dev eth0 | sed -n 's/^ *inet *\([.0-9]*\).*/\1/p'
It outputs clean IP-address.
 
Old 03-14-2008, 12:45 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,828

Rep: Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559
Given that loopback should always be 127.0.0.1

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

to put it into the variable
 
Old 03-14-2008, 03:17 AM   #11
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Eh.. just to be different
Code:
var=$(expr "`ifconfig eth0`" : '.*inet addr:\([^ ]*\).*')
But since you asked for awk/grep, here's yet another awk solution
Code:
ifconfig eth0| awk -F ' *|:' '/inet addr/{print $4}'

Last edited by angrybanana; 03-14-2008 at 03:32 AM.
 
Old 03-14-2008, 03:54 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
using only sed:
Code:
ifconfig | sed -n /'inet addr'/s'/.*addr:\([0-9.]\+\).*/\1/p'
var=$(ifconfig | sed -n /'inet addr'/s'/.*addr:\([0-9.]\+\).*/\1/p')
edit:
sorry i haven't seen jschiwal's code.. mine is just a dup of his

Last edited by konsolebox; 03-14-2008 at 03:59 AM.
 
Old 08-25-2012, 11:45 AM   #13
rocketero
LQ Newbie
 
Registered: Feb 2007
Posts: 5

Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by Hko View Post
I use this:
Code:
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')
or
var=$(ip -f inet addr show dev eth0 | sed -n 's/^ *inet *\([.0-9]*\).*/\1/p')
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use grep inside awk? Helene Programming 10 09-29-2015 09:48 PM
Help with Bash (grep/awk/etc) piercey Programming 12 02-27-2008 11:21 AM
Can you use grep / awk on variables instead of files? zest n zeal Linux - Newbie 2 02-11-2008 01:37 PM
Extracting text with grep or awk? UrbanDruid Linux - Newbie 5 04-07-2005 03:57 PM
newbie needs help for grep and awk parker Programming 1 08-12-2003 05:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration