简体   繁体   中英

Python: Search and Find a match a group of string

I'm trying to do a search and match especific string on a cisco router configuration that it is save on a .txt file. This is an example of part of the .txt file:

ip sla 101
udp-jitter 10.0.0.1 48092
request-data-size 64
tos 100
tag hostname
frequency 300
ip sla schedule 101 life forever start-time now
ip sla 102
udp-jitter 10.0.26.36 48092
request-data-size 64
tos 100
owner owner
tag hostname
frequency 300
ip sla schedule 102 life forever start-time now
ip sla 103
udp-jitter 10.0.114.1 48092
tos 100
vrf mskjhhj
owner owner2
tag hostname
ip sla schedule 103 life forever start-time now

So imagine 500 .txt file and all have different variation on the configuration, but all have must of the 'IP SLA 101, 102, 103 etc...'

The part that I want to grab and save is the configuration under each 'IP SLA '. For example the first line is 'ip sla 101' and the entire configuration for that is as:

ip sla 101
udp-jitter 10.0.0.1 48092
request-data-size 64
tos 100
tag xb02wepr01-004108
frequency 300
ip sla schedule 101 life forever start-time now

I want to be able to to search for the entire file and grab the information for each of the 'ip sla ' Remember that some of the file have different configuration the only this that it is the same is that when one of the ip sla is finish it is follow by another ip sla until there is no more. I don't know if I explain myself since it is Cisco configuration.

This is my code:

f =open('config.txt')
f =f.readlines()
f_out =open('save_result.txt', 'wb')
for i in f:
    if 'ip sla <any number>:
        ipsla<number>=[]
        ipsla<number>.append(i)
        ipslacurrent = i
    elif i != <any number>:
        ipsla<number>.append(i)
    f_out.write(ipsla<anynumber>)
f_out.close()

I hope all this make sence, if not I appologize. If there is any other way that will accomplishe this, I will appreciate. Probably with regular expression, but I have no experience with rgex. Thanks

UPDATE:

What I basically want is multiple variable. At the end of the script, I want the script to collect as the following example:

example:

ip_sla_101 =['udp-jitter 10.0.0.1 48092', 'request-data-size 64','tos 100','tag hostname','frequency 300','ip sla schedule 101 life forever start-time now']

ip_sla_102 =['udp-jitter 10.0.26.36 48092', 'request-data-size 64','tos 100','owner owner', 'tag hostname','frequency 300','ip sla schedule 102 life forever start-time now']

ip_sla_103 =['udp-jitter 10.0.114.1 48092','tos 100','owner owner2', 'tag hostname', 'ip sla schedule 103 life forever start-time now']

I'm not quite sure what you want, but I think you want to grab the entire line, in which case you could do:

with open('outfile.txt','w') as out_file:
    with open('infile.txt','r') as in_file:
        for line in in_file:
            if 'ip sla' in line:
                out_file.write(line)

We'll use just one simple regex, to recognize "ip sla " followed by numbers, but not letters:

import re
matcher = re.compile( "ip sla [0-9]+" )

This means: the literal string "ip sla ", followed by one or more characters from the set 0-9 (decimal digits).

Now, all you need to know is that matcher.match( s ) will tell you whether the string s matches the pattern or not.

You don't need to do anything fancy for the rest. Something like this would be one way to go about it:

collected = {} # a dictionary of first-lines to list-of-lines
current = []
for line in f.readlines():
    if matcher.match( line ): #this will match the line
        # we're starting a new section
        if current: # is there anything in the old section?
            collected[ current[0] ] = current # organize by first line
        current = [ line ] # start a new list for the new section
    else:
        current.append( line )
print collected[ "ip sla 101" ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM