mardi 30 mai 2017

Regex condition and extract

I'm parsing a file in bash, and I need to test if the current line is like this AND extract what's after "interface" :

interface EthernetXXXX/YYY

or

interface port-channelZZZZZ

where X, Y or Z is a number

Text sample :

 channel-group 105 mode active
   no shutdown

 interface Ethernet4/20
   description *** SW1-DT-A05-DC7 -> e1/37 ***
   switchport

For example :

$REGEX = "^interface (REGEX)"
if [[ $line =~ $REGEX ]];
then
    ifname = $XXX #the extracted part from the regex, i.e Ethernet4/20
fi

Do you know how to do this, if possible all in the if-statement ?

Aucun commentaire:

Enregistrer un commentaire