lundi 26 novembre 2018

Combining 2 files to create 1 file as output

I got 2 documents which look likes: First:

port2
port4
port10
etc.

Second:

port1
some stuff
about the port
I do not need
!
port2
some stuff
about the port
I really need
!
some generic stuff which is completely useless
!
port3
some stuff
about the port
I do not need
!
port4
some stuff
about the port
I really need
!
etc

Now, what I want is to create a loop that for each line in the first document we'll go through the second document and create a new file which contains all the data I need ("port2" until "!", "port4" until "!" etc)

What I got so far:

def access():
with open ("D:/portlist.txt") as f1, open ("D:/config.txt") as f2:
    match = False
    for line in f1:
        newConfig = open ("D:/portconfig.test.txt", "a")
        interface = line
        for line2 in f2:
            if re.match(interface, line2):
                newConfig.write(line2)
                print(line2)
                match = True
            elif re.match("!", line2):
                match = False   
            elif match:
                newConfig.write(line2)
        newConfig.close()   
access()

Problem is that the script stops after returning all about port2. It seems like the script doesn't return to the first loop to continue the process. Any ideas?

Aucun commentaire:

Enregistrer un commentaire