Python Forum
Grab and Parse a chunkc of text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grab and Parse a chunkc of text
#1
I mentioned a few times before that I am trying to learn/get better at python.

I am unable to load non standard modules.
I am using either python 2.6.6 or 2.7 (mostly 2.6.6)
To get a better handle on Python, I am trying to port my shell script over.

I ran into a snag. I have a shell script that cuts the following output from a log, then counts the number of

Quote:Stack# show version

Switch Ports Model SW Version SW Image
------ ----- ----- ---------- ----------
* 1 30 WS-C3750E-24PD 12.2(46)SE C3750E-UNIVERSAL-M
2 28 WS-C3750E-24PS 12.2(46)SE C3750E-UNIVERSAL-M
3 54 WS-C3750E-48TD 12.2(46)SE C3750E-UNIVERSAL-M

My bash script uses sed and grep to determine whether the device log Im looking at shows a stacked switch or not.

Sed
Quote:sed '/^Switch/,/^$/!d' xxx.log | grep -c "^"

The Sed response
Quote:6

I put the sed response in a var then perform if .. if larger than 3 I have a stacked switch.

My question is ..

How do I grab chunks of text with Python then count lines ?
Reply
#2
In Python I would generally use the re module (regular expressions) for this sort of thing. If you define your regular expression right, you can just to regex.findall(file_text), and that will return a list with all the matches. The len of that list gives you the number of matches.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Can you explain what the sed command does exactly here?
Reply
#4
(Oct-07-2019, 02:16 PM)Gribouillis Wrote: Can you explain what the sed command does exactly here?

searches for a line that starts with Switch
Quote:sed /^Switch/


through an empty line
Quote:sed /^Switch/,/^$/


do not delete the match / deletes everything but the match
Quote:sed /^Switch/,/^$/!d



from the log called xxx.log
Quote:sed /^Switch/,/^$/!d xxx.log


grep just counts start of lines
Quote:sed /^Switch/,/^$/!d xxx.log | grep -c "^"


Thanks
SUM
Reply
#5
I propose the use of a general function to grab a group of items from a sequence between the first item that satisfies a predicate to the first item that satisfies another predicate. You can easily write a small library for this
from pathlib import Path
import re

class EmptyGroup(RuntimeError):
    pass

def grab(seq, start, stop):
    """Grabs items in a sequence starting with the first
    item for which the predicate start(item) is True until
    the first item if any for which the predicate stop(item)
    is True.
    
    If no item is grabbed, raise the EmptyGroup exception.
    """
    seq = iter(seq)
    for x in seq:
        if start(x):
            yield x
            break
    else:
        raise EmptyGroup
    for x in seq:
        yield x
        if stop(x):
            return

def all_grabbed(seq, start, stop):
    """Generate items extracted from a sequence by calling
    repetively grab(seq, start, stop) until an empty group
    is returned.
    """
    seq = iter(seq)
    try:
        while True:
            yield from grab(seq, start, stop)
    except EmptyGroup:
        pass

def main():
    start = re.compile(r'^Switch').search
    stop = re.compile(r'^$').search
    with Path('paillasse/tmpswitch.log').open() as ifh:
        for x in all_grabbed(ifh, start, stop):
            print(repr(x))

if __name__ == '__main__':
    main()
Here is the contents of paillasse/tmpswitch.log
Output:
Stack# show version Switch Ports Model SW Version SW Image ------ ----- ----- ---------- ---------- * 1 30 WS-C3750E-24PD 12.2(46)SE C3750E-UNIVERSAL-M 2 28 WS-C3750E-24PS 12.2(46)SE C3750E-UNIVERSAL-M 3 54 WS-C3750E-48TD 12.2(46)SE C3750E-UNIVERSAL-M foo bar baz Switch Ports Model SW Version SW Image ------ ----- ----- ---------- ---------- * 1 30 WS-C3750E-24PD 12.2(46)SE C3750E-UNIVERSAL-M 2 28 WS-C3750E-24PS 12.2(46)SE C3750E-UNIVERSAL-M 3 54 WS-C3750E-48TD 12.2(46)SE C3750E-UNIVERSAL-M nooo
and here is the program's output
Output:
'Switch Ports Model SW Version SW Image\n' '------ ----- ----- ---------- ----------\n' '* 1 30 WS-C3750E-24PD 12.2(46)SE C3750E-UNIVERSAL-M\n' '2 28 WS-C3750E-24PS 12.2(46)SE C3750E-UNIVERSAL-M\n' '3 54 WS-C3750E-48TD 12.2(46)SE C3750E-UNIVERSAL-M\n' '\n' 'Switch Ports Model SW Version SW Image\n' '------ ----- ----- ---------- ----------\n' '* 1 30 WS-C3750E-24PD 12.2(46)SE C3750E-UNIVERSAL-M\n' '2 28 WS-C3750E-24PS 12.2(46)SE C3750E-UNIVERSAL-M\n' '3 54 WS-C3750E-48TD 12.2(46)SE C3750E-UNIVERSAL-M\n' '\n'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyMuPDF] Grab all strings of a given size? Winfried 3 599 Dec-26-2023, 07:39 AM
Last Post: Pedroski55
  Can't stop keyboard listener to grab chars typed inside CTk window Valjean 9 1,252 Sep-25-2023, 08:07 PM
Last Post: deanhystad
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 1,586 May-27-2022, 04:59 PM
Last Post: Winfried
  Screen capture opencv - grab error Kalman15 1 1,558 Jan-27-2022, 12:22 PM
Last Post: buran
  How can I parse a text file? Mike Ru 13 8,965 May-31-2017, 07:39 PM
Last Post: Mike Ru

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020