Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 08-28-2010, 01:47 PM   #1
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Rep: Reputation: 18
Separating Fields from Output - cut command


I want to display only the IP Address Value from the output of:

ifconfig eth0 | grep "inet addr"
as given below:

[root@localhost ~]# ifconfig eth0 | grep "inet addr"
          inet addr:  Bcast:  Mask:
[root@localhost ~]# ifconfig eth0 | grep "inet addr" | cut -d: -f2  Bcast
[root@localhost ~]#
but it is displaying the text "Bcast" as well.

The main problem here is that the first value and the second field / column name have no delimiter to separate them.

Okay, this one is working:

[root@localhost ~]# ifconfig eth0 | grep "inet addr" | cut -d: -f2 | cut -d" " -f1
[root@localhost ~]#
But can we have a better approach?
Old 08-28-2010, 02:05 PM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389

ifconfig eth0 | awk 'BEGIN{ FS="[ : ]"} /inet addr/ { print $13 }'

That is a tab (ctrl-v + tab) followed by a colon and a space between the brackets (the blue part).

The cut command can only have 1 specific delimiter, awk can have multiple. You can also use awk to "grep" for a pattern.

Hope this helps.
Old 08-28-2010, 02:10 PM   #3
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
This seems a little more elegant, but it could probably be improved as well.

ifconfig | sed -ne 's/.*inet addr:\(.*\)  B.*/\1/p'


p.s. was saying more elegant than the OP, not druuna. Got a phone call during post, and didn't refresh before submitting to se that post.

Last edited by forrestt; 08-28-2010 at 02:34 PM.
Old 08-28-2010, 02:23 PM   #4
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Another way in awk (portable to non-GNU awk versions):
/sbin/ifconfig eth0 | awk '/inet/{sub(/addr:/,"",$2); print $2}'


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I cut fields with repeated delimiters? Mountain Linux - Software 4 11-19-2012 07:50 AM
help with cut command using find. Cut last 8 characters leaving the rest ncsuapex Programming 4 09-16-2009 08:55 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
cut fields in a file christina_rules Linux - Newbie 12 07-15-2006 10:00 AM
separating syslog output by application mscott Linux - Software 0 03-08-2004 11:45 AM

All times are GMT -5. The time now is 02:10 AM.

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