title: Regex condition and extract
body: <p>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" :</p>
  
  <blockquote>
    <p>interface EthernetXXXX/YYY</p>
  </blockquote>
  
  <p>or</p>
  
  <blockquote>
    <p>interface port-channelZZZZZ</p>
  </blockquote>
  
  <p>where X, Y or Z is a number</p>
  
  <p>Text sample : </p>
  
  <pre><code> channel-group 105 mode active
     no shutdown
  
   interface Ethernet4/20
     description *** SW1-DT-A05-DC7 -&gt; e1/37 ***
     switchport
     switchport mode trunk
     switchport trunk native vlan 201
     switchport trunk allowed vlan 1-394,396-1609
  </code></pre>
  
  <p>For example :</p>
  
  <pre><code>$REGEX = "^interface (REGEX)"
  if [[ $line =~ $REGEX ]];
  then
      ifname = $XXX #the extracted part from the regex, i.e Ethernet4/20
  fi
  </code></pre>
  
  <p>Example :</p>
  
  <pre><code>#!/bin/bash
  
  filename = $1
  vlan = $2
  
  while read line; do
    echo $line
    $REGEX = "^interface (.*)"
    $REGEX_VLAN = "switchport.*$vlan.*"
    if [[ $line =~ $REGEX ]];
    then
        ifname = $XXX #the extraxted part from the regex
    else
      if [[ $line =~ $REGEX_VLAN ]];
      then
        echo "Interface $ifname contain the vlan $vlan"
    fi
  done &lt;$filename
  </code></pre>
  
  <p>Do you know how to do this, if possible all in the if-statement ?</p>