LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 08-28-2010, 02:47 PM   #1
Hi_This_is_Dev
Member
 
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


Hi,


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

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

Code:
[root@localhost ~]# ifconfig eth0 | grep "inet addr"
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
[root@localhost ~]# ifconfig eth0 | grep "inet addr" | cut -d: -f2
10.0.2.15  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:

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

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

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, 03:10 PM   #3
forrestt
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.

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

Forrest

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 03:34 PM.
 
Old 08-28-2010, 03:23 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

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


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 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 08:50 AM
help with cut command using find. Cut last 8 characters leaving the rest ncsuapex Programming 4 09-16-2009 09:55 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 08:16 PM
cut fields in a file christina_rules Linux - Newbie 12 07-15-2006 11:00 AM
separating syslog output by application mscott Linux - Software 0 03-08-2004 12:45 PM


All times are GMT -5. The time now is 10:13 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration