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" Code:
[root@localhost ~]# ifconfig eth0 | grep "inet addr" 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 |
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. |
This seems a little more elegant, but it could probably be improved as well.
Code:
ifconfig | sed -ne 's/.*inet addr:\(.*\) B.*/\1/p' 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. |
Another way in awk (portable to non-GNU awk versions):
Code:
/sbin/ifconfig eth0 | awk '/inet/{sub(/addr:/,"",$2); print $2}' |
All times are GMT -5. The time now is 10:04 PM. |