Take the 2-minute tour ×

I'm trying to get the list of networks interfaces from the command-line in this form:


I don't need to get them in a special order.

I can do this:


with this super non-optimized command, after having searched a lot and after I think I've reached my limits:

ifconfig -a | grep -E '' | sed -n 's/^\([^ ]\+\).*/\1/p' | tr -d '\n' | sed s/:/,/g | sed 's/.$//' 
share|improve this question

6 Answers 6

up vote 3 down vote accepted

I'll add this one to the pile. Add the quotes in the first sed, then just join the lines with paste.

ifconfig -a | sed -n 's/^\([^ ]\+\).*/"\1"/p' | paste -sd ","

This is better than tr since there's no trailing comma.

Better yet, since ifconfig -a output can't really be counted on to stay consistent, check /sys/class/net

ls /sys/class/net | sed -e 's/^\(.*\)$/"\1"/' | paste -sd ","
share|improve this answer
Interesting, but you still have : character –  sputnick Oct 4 '12 at 20:12
hmm, not on my flavor of ifconfig. asker should have posted the ifconfig -a output in the question, this seems to vary quite a bit from system to system. really should be grabbing this info from /sys/class/net or /proc/net/dev anyway. –  Tom McClure Oct 4 '12 at 20:23
ls /sys/class/net | sed -e 's/^(.*)$/"\1"/' | paste -sd "," This is perfect with "tr -d '\n'" on the end! Thanks! –  sidney Oct 5 '12 at 6:21

How about:

ifconfig -a | sed -e '/^ /d;/^$/d;s/\([^ ]*\) .*/"\1"/' | tr '\n' ','

There are three commands we're executing:

  1. /^ /d this command checks if the line starts with a space. If it does, "d" deletes it (and goes to the next line without executing the remaining commands).
  2. /^$/d this command deletes empty lines
  3. s/\([^ ]*\) .*/"\1"/ this command is reached when the above commands fail (ie., now we have a line that isn't empty and doesn't start with a space). What it does is capture as much characters that aren't spaces as it can, and then matches all characters from the space to the end of the line. The replacement string contains the special \1 "variable", which contains the captured string (ie. the interface name). We also include it between quotes.

After the sed command, we have the interfaces between double quotes, but one per line. To fix this we use tr '\n' ',' to replace newline characters with commas.

Hope this helps =)

share|improve this answer
There will be a pesky final comma to get rid of. –  tripleee Oct 5 '12 at 5:13
Yeah thanks, it helped me a lot, so I came up with this to get rid of the final coma: ifconfig -a | sed -e '/^ /d;/^$/d;s/([^ ]*) .*/"\1"/' | tr '\n' ',' however with this command there's still this character ':' before each comma –  sidney Oct 5 '12 at 6:09
ifconfig -a | awk '/^[^[[:space:]]/{a[NR]=$1} END{count=asort(a,b);for(i=1;i<count;i++){printf "\"%s\",", b[i]}; printf "\"%s\"\n", b[count]}'
share|improve this answer

With Perl in a shell :

ifconfig |
perl -ane '
    END{print "\042", join("\042,\042", @a), "\042\n"}
    $a[$c++] = $1 if /^(\w+)/; 

Try the following if you use bash :

x=( $(ifconfig | grep -o '^[[:alnum:]]\+') )

for ((i=0; i<${#x[@]} -1; i++)); do
    printf '"%s",' ${x[i]}

printf '"%s"\n' ${x[-1]}

Based on Tom McClure one but without parsing ls :

printf '"%s"\n' $(
    printf '%s\n' /sys/class/net/* | cut -d '/' -f 5
) | paste -sd ","
share|improve this answer
Added last one. –  sputnick Oct 4 '12 at 21:20

One way using GNU find and /sys/class/net/:

find /sys/class/net/ ! -type d -printf "\"%f\"," | sed 's/,$/\n/'

One way parsing ifconfig -a:

ifconfig -a | sed -ne '/^[^ ]/ { s/^\([^ ]\+\).*/"\1"/; H }' -e '$ { g; s/\n//; s/\n/,/g; p }'
share|improve this answer

It might be better to parse the corresponding nodes in the /sys filesystem if you have that, but try

ifconfig -a |
awk '/^[^ ]/ { printf ("%s\"%s\"", s, $1); s="," }'

If there's a trailing colon on the interface name field, you can fix that by using awk -F:.

share|improve this answer

Your Answer


