A clever use of pipes is usually the answer. For example you can use the raw ifconfig
output:
$ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.4 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::ca60:ff:fe06:ac5b prefixlen 64 scopeid 0x20<link>
ether c8:60:00:06:ac:5b txqueuelen 1000 (Ethernet)
RX packets 622902 bytes 792538068 (755.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 392538 bytes 37764707 (36.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
And then carve away this output until you see the information you are after:
$ ifconfig eth0 | grep -w inet | cut -d ' ' -f 10
192.168.0.4
My own choice of weapons:
- Use grep with the
-w
option to search for only whole word matches. This way we do not catch inet6
as well.
- Use
cut
with a delimiter (-d
) of a space and then select the appropriate field (-f
) number to get the information we are after.
There are many, many tools to do this and doubtless a better way to accomplish this particular job. But this example shows the use of pipes and several of the tools that you can use.
grep
andcut
to isolate a known piece of output. You can pipe (|
) the output to these commands. Seeman grep
andman cut
. – amc Jul 8 '16 at 6:28awk
,head
,tail
,cut
, you name it. Then you can also useperl
,python
, i.c.wregex
or not ... There is not a one fits all answer to it. – Jacob Vlijm Jul 8 '16 at 6:42