Home > Mobile >  SED style Multi address in Python?
SED style Multi address in Python?

Time:09-30

I have an app that parses multiple Cisco show tech files. These files contain the output of multiple router commands in a structured way, let me show you an snippet of a show tech output:

`show clock`
20:20:50.771 UTC Wed Sep 07 2022
Time source is NTP
`show callhome`
callhome disabled
Callhome Information:
<SNIPET>
`show module`
Mod Ports             Module-Type                      Model           Status
--- ----- ------------------------------------- --------------------- ---------
1    52   16x10G   32x10/25G   4x100G Module    N9K-X96136YC-R        ok        
2    52   16x10G   32x10/25G   4x100G Module    N9K-X96136YC-R        ok        
3    52   16x10G   32x10/25G   4x100G Module    N9K-X96136YC-R        ok        
4    52   16x10G   32x10/25G   4x100G Module    N9K-X96136YC-R        ok        
21   0    Fabric Module                         N9K-C9504-FM-R        ok        
22   0    Fabric Module                         N9K-C9504-FM-R        ok        
23   0    Fabric Module                         N9K-C9504-FM-R        ok        
<SNIPET>

My app currently uses both SED and Python scripts to parse these files. I use SED to parse the show tech file looking for a specific command output, once I find it, I stop SED. This way I don't need to read all the file (these can get to be very big files). This is a snipet of my SED script:

sed -E -n '/`show running-config`|`show running`|`show running config`/{
            p
            :loop
            n
            p
            /`show/q
            b loop
        }' $1/$file

As you can see I am using a multi address range in SED. My question specifically is, how can I achieve something similar in python? I have tried multiple combinations of flags: DOTALL and MULTILINE but I can't get the result I'm expecting, for example, I can get a match for the command I'm looking for, but python regex wont stop until the end of the file after the first match. I am looking for something like this

sed -n '/`show clock`/,/`show/p'

I would like the regex match to stop parsing the file and print the results, immediately after seeing `show again , hope that makes sense and thank you all for reading me and for your help

CodePudding user response:

You can use nested loops.

import re

def process_file(filename):
    with open(filename) as f:
        for line in f:
            if re.search(r'`show running-config`|`show running`|`show running config`', line):
                print(line)
                for line1 in f:
                    print(line1)
                    if re.search(r'`show', line1):
                        return

The inner for loop will start from the next line after the one processed by the outer loop.

You can also do it with a single loop using a flag variable.

import re

def process_file(filename):
    in_show = False
    with open(filename) as f:
        for line in f:
            if re.search(r'`show running-config`|`show running`|`show running config`', line):
                in_show = True
            if in_show
                print(line)
                if re.search(r'`show', line1):
                    return
  • Related