Search code examples
bashawkgrepcut

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


Solution

  • 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.