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/.$//'
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 ","
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 =)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With