grepcisco

grep cisco interfaces without any configuration


This is sample of Cisco Switch configuration file. If the interface is not in use, it should be in shutdown mode.

config.txt

!
interface GigabitEthernet0/0
 shutdown
!
interface GigabitEthernet0/1
!
interface GigabitEthernet0/2
 shutdown
!
interface GigabitEthernet0/3 
!

Therefore, I would like to grep any interfaces without any configuration and no shutdown in it.

Desired Output

!
interface GigabitEthernet0/1
!
!
interface GigabitEthernet0/3 
!

Can I do something like grep interface.*[0-9] config.txt where the line before and after it must match !

Here are a few of my attempts, but none of them producing the output that I wanted.

grep interface.*[0-9] config.txt
grep -C1 interface.*[0-9] config.txt

If there is a better solution then grep, please let me know.


Solution

  • I suggest using a GNU grep (or pcregrep if you have no access to GNU grep) solution:

    grep -Poz '(?m)^!\R\Kinterface.*\R(?=!$)' file
    

    See the online grep demo and the regex demo.

    Details