0

I need to show the ethernet interfaces on a Centos 7 server. On centos 6.5, I was using this command (from another post)and it works fine on 6.5.

ifconfig -a | sed  -n 's/^\([^ ]\+\).*/\1/p' | grep -Fvx -e lo 

on Centos 7 the interface output now has a trailing ":"

so my output look like this

ifconfig -a | sed  -n 's/^\([^ ]\+\).*/\1/p' | grep -Fvx -e lo
ens32:
lo:

to remove the "lo:" I modified to this. which results in ens32:

ifconfig -a | sed  -n 's/^\([^ ]\+\).*/\1/p' | grep -Fvx -e lo:
ens32:

How can I remove the trailing ":" ? can this sed be modified? or other ideas

5
  • of course 2 minutes after i posted this i found a solution ifconfig -a | sed -n 's/^([^,: ]\+).*/\1/p' | grep -Fvx -e lo: Commented Aug 10, 2016 at 17:33
  • 1
    at least provide the answer you found and mark it as accepted answer for others to benefit from your finding, in the future. Commented Aug 10, 2016 at 17:36
  • 2
    Don't you have ip on that system ? Commented Aug 10, 2016 at 17:37
  • ifconfig -a | sed -n 's/^([^,: ]\+).*/\1/p' | grep -Fvx -e lo: Commented Aug 10, 2016 at 17:47
  • sed -rn '/^ |^lo/!s/ /\n/;/\n/P' == sed -rn 's/(^lo)? /\n/;/^[[:alnum:]]/P;' Commented Aug 10, 2016 at 18:28

2 Answers 2

0

It is unfortunate that you don't provide the raw output of the ifconfig command. But I would go to something like that :

$ ifconfig -a | perl -anle 'print $F[0] if /(^\w)/'
$  enp2s0f0
   lo
   tun0
   wlp3s0b1

Raw output is the following:

enp2s0f0  Link encap:Ethernet  HWaddr 3c:07:54:5f:5e:35  
          inet addr:192.168.1.114  Bcast:192.168.1.255 
[...] 

perl -anle, where is 'a' split the line in column and fill the variable $F 'n' is to process the output line by line, 'l' is to put a new line between every print and 'e' is to execute the perl command in command line. Then follow the regex where I simply say that I want to match the line '//' that contains one char '\w' at the beginning of the line '^'.

0

This will work on old systems without the colon and on new systems with the colon:

$ /sbin/ifconfig -a | awk -F'[ :]+' '$1 && $1!="lo" {print $1}'
eth0

Alternatively, using sed, this will also work on both new and old systems:

$ /sbin/ifconfig -a | sed -n 's/[ :].*//; /^lo$/t; /./p'
eth0

When awk or sed are used, it is rare to need an additional process to run grep.

How the awk command works

  • -F'[ :]+'

    This tells awk to treat any consecutive blanks or colons as a field separator.

  • $1 && $1!="lo" {print $1}

    If there is a first field and if that field is not lo, then print it.

How the sed command works

  • -n

    This tells sed not print anything unless explicitly asked to.

  • s/[ :].*//

    This tells sed to remove everything from the first space or colon to the end of the line.

  • /^lo$/t

    If the line matches lo, then skip the rest of the commands and start over on the next line.

  • /./p

    If the line contains any characters at all, print the line.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.