Parse interfaces with ifconfig

I am trying to get a list of network interfaces from the command line in this form:

"wlan0","eth0","lo" 

I do not need to receive them in a special order.

I can do it:

 wlan0,eth0,lo 

with this super-optimized team, after much searching and after I think I have reached my limits:

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

I will add this to the heap. Add quotation marks to the first sed, then just append the lines with the paste.

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

This is better than tr because there is no trailing comma.

Even better, since ifconfig -a output cannot be considered constant, check / sys / class / net

 ls /sys/class/net | sed -e 's/^\(.*\)$/"\1"/' | paste -sd "," 
+4
source

What about:

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

There are three commands that we execute:

  • /^ /d this command checks to see if a line begins with a space. If so, "d" deletes it (and moves on to the next line without executing the remaining commands).
  • /^$/d this command removes empty lines
  • s/\([^ ]*\) .*/"\1"/ this command is achieved when the above commands fail (that is, we now have a line that is not empty and does not start with a space). What he does is to capture as many characters as possible, which are not spaces, and then match all the characters from space to the end of the line. The replacement string contains a special \1 "variable that contains the captured string (that is, the name of the interface). We also include it between quotation marks.

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

Hope this helps =)

+3
source
 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]}' 
+2
source

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 are using 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 but indiscriminately ls:

 printf '"%s"\n' $( printf '%s\n' /sys/class/net/* | cut -d '/' -f 5 ) | paste -sd "," 
+2
source

It might be better to parse the corresponding nodes in the /sys file system if you have this, but try

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

If there is a trailing colon in the interface name field, you can fix this using awk -F:

+1
source

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

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

One way to parse ifconfig -a :

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

If you only need a list of interfaces, this should work:

 echo $(ifconfig | sed -n '/^[[:alnum:]]*:/s/:.*//p') 

If you really need commas between the elements, you can add the following channel:

 | sed 's/ /,/g' 

and if you really need double quotes and commas, you can add the following feed:

 | sed -e 's/ /","/g' -e 's/.*/"&"/' 
+1
source

Source: https://habr.com/ru/post/1437872/


All Articles