Python - Suggestion for a python script - match lines

Posted on 2012-09-08
Last Modified: 2012-09-11
I'm thinking about creating a PY script that would match the bold elements (this is a Linux command output):

# multipath -l
mpath1 (3600d0230003228bc000339414edb8101)
[size=10 GB][features="0"][hwhandler="0"]
\_ round-robin 0 [prio=1][active]
 \_ 2:0:0:6 sdb 8:16 [active][ready]
\_ round-robin 0 [prio=1][enabled]
 \_ 3:0:0:6 sdc 8:64 [active][ready]

Is there a way to match the row that :
begins with \_
continues with number:number:number
 sdc 8:64 are not important
and match [active][ready] ?

It's a monitoring script. Purpose is to see if active and ready statements change.

Question by:g0all
    LVL 16

    Accepted Solution

    If I understood coorectly:
    You plan to write a Python script, that starts multipath -l and parses it's output and does something whenever it encounters an 'intersting' line.

    I'll give you a solution assuming, that the process multipath is a process, that terminates quickly and that does not create gigabytes of output.
    If the result of this command were long, then I would use generators instead of reading the output into a list.
    def get_command_output():
        """ """
        cmd = ['multipath', '-l']
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        result = proc.stdout.readlines()
        return result 
    def find_intersting_lines():
        lines = get_command_output()
        # Te next command with the regular expression string 
        # could be written in one line.
        # Splitting it over multiple lines allows to comment it
        active_ready_re_str = ( 
            r'^\s*'                # potentially leading white space at 
                                   # beginning of line.
                                   # It existed at least in the example,that you
                                   # pasted
            r'\\_\s'               # \_ folowed by one white space character
            r'(\d+:){3}\d+\s'      # num:num:num:num folowed by a space character
            r'.*'                  # whatever
            r'\[active\]\[ready\]$'# [active][ready] followed by end of line
        active_ready_re = re.compile(active_ready_re_str)
        for line in lines:
            if re.match(active_ready_re, line):
                print '!!!', line,

    Open in new window

    LVL 16

    Expert Comment

    Here as generator, which might be more useful if the 'multipath'
     is ronning for a long time and prints out the results over time.

    def get_command_output():
        """ """
        cmd = ['multipath', '-l']
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        for line in proc.stdout:
            yield line

    Open in new window

    LVL 1

    Author Comment

    Thank you,  gelonida.

    Yeah. It's obvious that this is too much for me. I'd better take a python book and read some more about this language.

    Meanwhile, I would have some newbie questions...

    What is subprocess.Popen and stdout=subprocess.PIPE ?

    Why this is considered as a suprocess?

    Thanks again!
    LVL 16

    Expert Comment

    subprocess is the library, that you should use if you want to start other processes.

    In your case the command is "multipath -l'

    as you want to read the output of this command you have to tell python to pass the stdout of this subprocess to a PIPE.
    THis means, that you can then read the stdout of that process by reading proc.stdout.

    whithout the 'stdout=sunprocess.PIPE'  option the output would just be sent to stdout (the our console)

    For more info just google for python and subprocess and amongst other you should find.
    explaining some more details.

    The regular expressions, that I used to match your line are explained in the documentation of the module re. (  )

    Regular expressions are very powerful, but a little tricky to learn.

    regular expressions are also used by sed / awk / grep / vi / perl and many other tools.
    The exact features of regular expressions do vary however.

    So after reading any good introduction / tutorial about regular expressions be sure to read what exactly is supported by python or any other tool you'd like to use.
    LVL 1

    Author Comment

    Now it makes sense. Many thanks.
    LVL 16

    Expert Comment

    just for your Info.
    I edited the previous message to add some info about regular expressions.

    Not sure, whether you replied before or after I added the additional comments.
    Wish you happy pythoning and a nice sunday.
    LVL 1

    Author Comment

    I've replied after, I will read informations you've suggestion.

    Have a great day too.
    LVL 1

    Author Comment


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now