1
ip a | egrep '([0-9]{1,3}\.){3}[0-9]{1,3}|enp0s' | grep -e 10 -e 172 -e 192 | awk '{print $2}' | awk '{gsub("/24", "");print}' | xargs

The above command gives the output as:

enp0s3: 10.0.2.15 enp0s8: 192.168.56.100

I want the output to be printed in 2 different lines starting with Network card names.

How can I edit the output to get the desired result??

  • Then remove | xargs in end – αғsнιη Jun 24 '18 at 2:50
  • enp0s3: 10.0.2.15 enp0s8: 192.168.56.100 If I remove xargs, I get 4 lines. I want output as Network Card: IP address in 2 different lines – Vivek Dabas Jun 24 '18 at 2:54
  • What is the output of ip a? (I'm not on a Linux system). – Kusalananda Jun 24 '18 at 8:05
  • change |xargs to |paste - - – glenn jackman Jun 24 '18 at 11:14
1

I think what you're looking for is

 ip a | awk '/inet .*enp[0-9]*s[0-9]*/ { print $NF " " gensub(/\/[0-9]*/,"",1,$2); }'

Note that I modified the match for the interface name to accept more than enp0s (On my system many of my interfaces are enp2s*). Also, I'm matching lines with 'inet ', this will exclude ipv6 addresses ( 'inet6 ' ).

0

A quick and dirty solution is to give xargs a -L 2 option.  It would be better simply to do it all in awk; it’s very powerful.

You realize that the -e 10 search string given to grep could easily match an Ethernet address or an IPv6 address.

  • Thanks, it worked. It would be great if you can show by using awk as well. – Vivek Dabas Jun 24 '18 at 3:26

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.