简体   繁体   中英

How to search for multiple strings in a file with multiline string

Trying to write a script that will search an entire file for certain strings.

For more than 2 strings.

1) First search is to check for either 1 of the following two lines:

0/RP1/CPU0    RP(Active)

Or

0/RP0/CPU0    RP(Active)

If ' 0/RP1/CPU0 RP(Active) ' then print this message " execute command location 0/rp1/cpu0 "

If ' 0/RP0/CPU0 RP(Active) ' then print this message " execute command location 0/rp0/cpu0 "

2) Second search is to check for the either 1 of the following multi-line: a)

INFO_LINE------------------: TITLE_LINE(A-Z)
  State                              : ENABLED

b)

INFO_LINE------------------: TITLE_LINE(A-Z)
  State                              : DISABLE

The ' TITLE_LINE(AZ) ' could differ slightly but INFO_LINE will be static and the same in either ENABLED or DISABLE .

If b) is true then print " restart process on location (FROM SEARCH1) .

I have tried if/else/elif statements and have been researching using the re.search for regular expressions.

#!/usr/bin/python
activerp = open('sample-output.txt')

def check_active_rp():
    for line in activerp:
        if line.find('0/RP1/CPU0    RP(Active)'):
           print("execute command location 0/rp1/cpu0")
        else: 
           if line.find('0/RP0/CPU0    RP(Active)'):
            print("execute command location 0/rp0/cpu0")

running this script python just returns me back to cli prompt and I couldnt get further to complete the other search.

CLI$ python test.py CLI$

I think this is what you want:

def check_active_rp():
   string = '0/RP1/CPU0    RP(Active)'
   for line in activerp:
      if string in line:
         print('execute command location 0/rp1/cpu0')

I created a file containing the strings you were searching and did some testing, and your example should have given you some output, albeit wrong. It got me thinking that you don't have a full grasp on python scripts, but correct me if I'm wrong.

In order for your function to be executed you need to call it. Writing def simply defines it. You can find more about it here .

I see you're looking at regex for this, but if there are no variations in the string you're searching for you can just use the find function.

The thing is that line.find() returns an integer and not a boolean value. So you would always enter the first if statement, unless your line started with '0/RP1/CPU0 RP(Active)' (as then it would return the 0 index). If we check the documentation we can see that find function returns -1 if no string is found. So you could change your if statement with something like this: line.find('0/RP1/CPU0 RP(Active)') != -1 . The same can be done with the multiline strings. The only thing is that you need to dump the whole file in a string. So with that in mind this is the solution that could solve the problem.

def check_active_rp(activerp):
    whole_file = activerp.read()

    if whole_file.find('0/RP1/CPU0    RP(Active)') != -1:
        print("execute command location 0/rp1/cpu0")
    elif whole_file.find('0/RP0/CPU0    RP(Active)') != -1:
        print("execute command location 0/rp0/cpu0")

    if whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)\n  State                              : ENABLED') != -1:
        print('state is ENABLED')
    elif whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)\n  State                              : DISABLE') != -1:
        print('restart process on location (FROM SEARCH1)')


with open('sample-output.txt') as active_rp:
    check_active_rp(active_rp)

In your example you also never close the file, so I used the with statement which is considered good practice when dealing with IO.

UPDATE:

I just figured out you would like to change what is written in info line, in that case the use of regex is appropriate. The following solution would then work:

import re

def check_active_rp(activerp):
    iterator = iter(activerp)
    for line in iterator:
        if line.find('0/RP1/CPU0    RP(Active)') != -1:
            print("execute command location 0/rp1/cpu0")
        elif line.find('0/RP0/CPU0    RP(Active)') != -1:
            print("execute command location 0/rp0/cpu0")

        pattern = re.compile('INFO_LINE------------------: ([A-Z]+)')

        x = pattern.search(line)

        if x:
            line = next(iterator)
            if line.find('ENABLED') != -1:
                print('the {} is ENABLED'.format(x.group(1)))
            elif line.find('DISABLE') != -1:
                print('the {} is DISABLED'.format(x.group(1)))


So we create an iterator out of the file and start going line by line through the file. We still use the string find function for the first string search. Now we continue on to the INFO LINE. Using the regex package we compile a regex that would capture the TITLE_LINE. Once that is found we get the next line from the iterator and once again check if the string contains ENABLED or DISABLE; and print accordingly.

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