locklockM
locklockM

Reputation: 139

bash fetch first field awk grep

In my brctl show command :

bridge name bridge id       STP enabled interfaces
olm-com_uim     8000.b8ca3a5ecab1   no      eth1
                            vnet1


tyy-fom_psr     8000.a0369f11b218   no      bond1103
                            vnet0
                            vnet10
                            vnet8
uuu-r8s_udm     8000.b8o        eth1.1621
                            vnet5
bbb-r8s_ptr     8000.b8c    no      bond1115

I just want to grep :

olm-com_uim
tyy-fom_psr
uuu-r8s_udm 
bbb-r8s_ptr

So I try this,

brctl show | grep -v vnet | grep -v bridge | awk '{print $1}'

But I think, its is no very well method

Upvotes: 2

Views: 106

Answers (5)

glenn jackman
glenn jackman

Reputation: 247210

Lots of answers. Here's sed:

brctl show | sed -e 1d -nre 's/^([^[:blank:]]+).*/\1/p'

Upvotes: 0

iamauser
iamauser

Reputation: 11489

brctl output from my dev box. You should get the desired result with the following command.

$ brctl show | awk 'NF>1 && NR>1{print $1}'
br0
docker0
virbr0
virbr1
virbr2
virbr3

NF>1 is needed to filter out the vnet interfaces that follows the bridge interfaces.

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 204608

$ awk -F'\t' 'NR>1 && $1{print $1}' file
olm-com_uim
tyy-fom_psr
uuu-r8s_udm
bbb-r8s_ptr

or if your fields weren't tab-separated:

$ awk 'NR>1 && /^[^[:space:]]/{print $1}' file
olm-com_uim
tyy-fom_psr
uuu-r8s_udm
bbb-r8s_ptr

Upvotes: 1

anubhava
anubhava

Reputation: 786136

This can be done with grep -o as well:

brctl show | tail -n +2 | grep -o '^[^[:blank:]]\+'

olm-com_uim
tyy-fom_psr
uuu-r8s_udm
bbb-r8s_ptr

Regex ^[^[:blank:]]\+ matches 1+ non-whitespace characters at the line start.

Upvotes: 2

karakfa
karakfa

Reputation: 67567

another awk

$ awk '!/^ / && NF{print $1}' file

olm-com_uim
tyy-fom_psr
uuu-r8s_udm
bbb-r8s_ptr

if not starting with empty and not blank line print first field. If you want to skip header add && NR>1

Upvotes: 1

Related Questions