user27396 user27396 - 6 months ago 5
Bash Question

How to extract the rows under a column using shell script

I have following output of linux command brctl show. I would like to extract all the associated interfaces (as given in the interface column) and store it in some array. Can somebody suggest a way to achieve this?

root@XXXX:~# brctl show
bridge name bridge id STP enabled interfaces
br-lan 7fff.00c0ca7e0288 no eth0

I am missing the exact formatting for some reason. But the output of brctl show looks pretty much like above with the exception of a few spacings.

So I would like to store eth0,wlan1_1, w;an1_1.sta1 in some array if possible.



You can use cut and tail. You may have to adjust the number:

brctl show | tail -n +2 | cut -c 36-

If the tab characters are converted to spaces in your output, you will need:

brctl show | tail -n +2 | cut -c 46-

Bash script solution

Note: I tested with a datafile of your data, but it should work with the brctl show output as well:


declare -i cnt=0
declare -a ifaces

while read -r line || [ -n "$line" ]; do
    [ $cnt -eq 0 ] && { ((cnt++)); continue; }
    ifaces+=( "${line//* /}" )
done <<<$(brctl show)

printf "%s\n" ${ifaces[@]}

tested with done <"$1" as substitute:

$ bash dat/brctl.txt