Search code examples
regexperlcisco

Regex match line above (Perl)


I want to write a simple script which gives me from a Cisco Configuration every Port which is not in use (shutdown). The shutdown Ports looks always the same like:

interface Ethernet100/1/40
  shutdown

Which Regex can I use to get always the line above 'shutdown' ? I want to write this script in Perl, please help me.


Solution

  • Turn multiline option on and use this:

    ^.+$(?=\s+shutdown)
    

    That will match the line before the word shutdown.

    It's basically looking for a line of any characters ^.+$, looking ahead for some whitespace followed by shutdown.

    Try it here: https://regex101.com/r/WFqX5A/1