Reputation: 2714
I'm trying to get the list of networks interfaces from the command-line in this form:
"wlan0","eth0","lo"
I don't need to get them in a special order.
I can do this:
wlan0,eth0,lo
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/.$//'
Upvotes: 4
Views: 3712
Reputation: 178
If you just want a list of interfaces, this should work:
echo $(ifconfig | sed -n '/^[[:alnum:]]*:/s/:.*//p')
If you really need the commas between the items, then you could add the following pipe:
| sed 's/ /,/g'
and if you really want double quotes and commas, then you could add the following pipe:
| sed -e 's/ /","/g' -e 's/.*/"&"/'
Upvotes: 1
Reputation: 189749
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:
.
Upvotes: 1
Reputation: 54542
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 }'
Upvotes: 1
Reputation: 185560
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]}
done
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 ","
Upvotes: 2
Reputation: 6739
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 ","
Upvotes: 4
Reputation: 5072
How about:
ifconfig -a | sed -e '/^ /d;/^$/d;s/\([^ ]*\) .*/"\1"/' | tr '\n' ','
There are three commands we're executing:
/^ /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)./^$/d
this command deletes empty liness/\([^ ]*\) .*/"\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 =)
Upvotes: 3
Reputation: 15239
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]}'
Upvotes: 2