How can I shorten the data that I retrieve once I enter a command? For example if I input:

ifconfig -a getifaddr

I only want the IP address, nothing else. Surely there is something else that I can add to my command to simply pinpoint answers in the mass flow of text that is retrieved?

FYI the example command was simply an example

  • 1
    I usually use a combination of grep and cut to isolate a known piece of output. You can pipe (|) the output to these commands. See man grep and man cut. – amc Jul 8 '16 at 6:28
  • 1
    ^ What he said, but there are many, many more tools to specifically parse out information: awk, head, tail, cut, you name it. Then you can also use perl, python, i.c.w regex or not ... There is not a one fits all answer to it. – Jacob Vlijm Jul 8 '16 at 6:42

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  netmask  broadcast
        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

My own choice of weapons:

  1. Use grep with the -w option to search for only whole word matches. This way we do not catch inet6 as well.
  2. 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.


To add to andrew.46s answer here is how you can do it with awk and sed. Needs as well the clever use of pipes.

ifconfig -a | awk '/inet/{print $2;}' | sed 's/addr://'



enp2s0    Link encap:Ethernet  HWaddr 18:a9:05:e3:f6:ca  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:  Mask:
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:13845 errors:0 dropped:0 overruns:0 frame:0
          TX packets:13845 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:1594329 (1.5 MB)  TX bytes:1594329 (1.5 MB)

wlp6s0    Link encap:Ethernet  HWaddr c4:17:fe:20:10:ed  
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::c617:feff:fe20:10ed/64 Scope:Link
          RX packets:104772 errors:0 dropped:0 overruns:0 frame:3603935
          TX packets:78681 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:90707841 (90.7 MB)  TX bytes:14272953 (14.2 MB)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.