Search code examples
pythonloopswhile-loop

Loop through file, when regex match is found append it on another line until the next iteration is found


I've tried a few different approaches and still working at it but maybe I can get pointed quickly for the sake of time with this project. Any advice is appreciated.

Loop through file and anytime I see ===r(xxxx).(xxxx).(xxxx) I need to append r(xxxx).(xxxx).(xxxx) to each line that has the word remark on the line until it hits the next line that includes remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=, then rinse and repeat until end of file...

Example file .txt to loop:
access-list r1999-outside-in remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
access-list r1999-outside-in remark ===r9920.4001.886
access-list r1999-outside-in remark Access from Test Network
access-list r1999-outside-in extended permit tcp xxx.xxx.xxx.xxx 255.255.255.128 x.x.32.160 255.255.255.248 eq 4343 
access-list r1999-outside-in remark auth1.this.that
access-list r1999-outside-in extended permit ip host x.x.33.39 x.x.32.160 255.255.255.248 
access-list r1999-outside-in remark Access to Mgmt from  VPN
access-list r1999-outside-in extended permit tcp object-group VPN-NETWORK-NO-PROXY x.x.x.x 255.255.255.248 eq 4343 
access-list r1999-outside-in remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
access-list r1999-outside-in remark ===r9930.9975.1296 - Wireless Controllers
access-list r1999-outside-in extended permit object-group TFTP host x.x.33.203 x.x.33.160 255.255.255.248 
access-list r1999-outside-in remark Access to Mgmt from  VPN
access-list r1999-outside-in extended permit tcp object-group VPN-NETWORK-NO-PROXY x.x.x.x 255.255.255.248 eq 4343 
access-list r1999-outside-in remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
access-list r1999-outside-in remark ===r9940.4001.886 - Wireless Controllers
access-list r1999-outside-in remark Access from Test Network
access-list r1999-outside-in extended permit tcp xxx.xxx.xxx.xxx 255.255.255.128 x.x.32.160 255.255.255.248 eq 4343 
access-list r1999-outside-in extended permit tcp 10.x.x.0 255.255.252.0 x.x.32.160 255.255.255.248 eq 4343 
access-list r1999-outside-in remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


Snippet of a block with appended text

access-list r1999-outside-in remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
access-list r1999-outside-in remark ===r9920.4001.886
access-list r1999-outside-in remark Access from Test Network r9920.4001.886
access-list r1999-outside-in extended permit tcp xxx.xxx.xxx.xxx 255.255.255.128 x.x.32.160 255.255.255.248 eq 4343 
access-list r1999-outside-in remark auth1.this.that r9920.4001.886
access-list r1999-outside-in extended permit ip host x.x.33.39 x.x.32.160 255.255.255.248 
access-list r1999-outside-in remark Access to Mgmt from  VPN r9920.4001.886
access-list r1999-outside-in extended permit tcp object-group VPN-NETWORK-NO-PROXY x.x.x.x 255.255.255.248 eq 4343 
access-list r1999-outside-in remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
access-list r1999-outside-in remark ===r9930.9975.1296

do the same thing here we did above....

access-list r1999-outside-in remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

And to get a little more complicated my next task is to replace all the r(xxxx) with a new word.

I have a text file that has the new referenced data in it like this... I can focus on this next though but any advice would be nice as well. r1130,new_word r1140,new_word

Im looping through and getting the first regex results but having issues after that. Just a little advice would be nice for the next few approaches.

with open("file.txt", "r") as file:
# Create an empty list to store the lines
    lines = []

    # Iterate over the lines of the file
    for line in file:
        line = line.strip()
        # Append the line to a list
        lines.append(line)
        # regex to pull out r(xxxx).(xxxx).(xxxx)
        y = re.search("(r\d{4}.\d+.\w+)", line)
        if y != None:
            print(y)

Solution

  • I think this is what you're after. No regular expressions are necessary here. You just track the current suffix, reset it when you find =-=-=-= and set a new one when you find "remark ===".

    suffix = ""
    with open("x.txt") as file:
    # Create an empty list to store the lines
        # Iterate over the lines of the file
        for line in file:
            line = line.strip()
            if '=-=-=-=' in line:
                suffix = ''
                print(line)
            elif 'remark ===' in line:
                suffix = ' ' + line.split()[3]
                print(line)
                continue
            elif 'remark' in line:
                print( line + suffix )
            else:
                print(line)
    

    Output:

    access-list r1999-outside-in remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    access-list r1999-outside-in remark ===r9920.4001.886
    access-list r1999-outside-in remark Access from Test Network ===r9920.4001.886
    access-list r1999-outside-in extended permit tcp xxx.xxx.xxx.xxx 255.255.255.128 x.x.32.160 255.255.255.248 eq 4343
    access-list r1999-outside-in remark auth1.this.that ===r9920.4001.886
    access-list r1999-outside-in extended permit ip host x.x.33.39 x.x.32.160 255.255.255.248
    access-list r1999-outside-in remark Access to Mgmt from  VPN ===r9920.4001.886
    access-list r1999-outside-in extended permit tcp object-group VPN-NETWORK-NO-PROXY x.x.x.x 255.255.255.248 eq 4343
    access-list r1999-outside-in remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    access-list r1999-outside-in remark ===r9930.9975.1296 - Wireless Controllers
    access-list r1999-outside-in extended permit object-group TFTP host x.x.33.203 x.x.33.160 255.255.255.248
    access-list r1999-outside-in remark Access to Mgmt from  VPN ===r9930.9975.1296
    access-list r1999-outside-in extended permit tcp object-group VPN-NETWORK-NO-PROXY x.x.x.x 255.255.255.248 eq 4343
    access-list r1999-outside-in remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    access-list r1999-outside-in remark ===r9940.4001.886 - Wireless Controllers
    access-list r1999-outside-in remark Access from Test Network ===r9940.4001.886
    access-list r1999-outside-in extended permit tcp xxx.xxx.xxx.xxx 255.255.255.128 x.x.32.160 255.255.255.248 eq 4343
    access-list r1999-outside-in extended permit tcp 10.x.x.0 255.255.252.0 x.x.32.160 255.255.255.248 eq 4343
    access-list r1999-outside-in remark =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=