google / textfsm

Python module for parsing semi-structured text into python tables.
Apache License 2.0
1.09k stars 168 forks source link

How to loop over multiple newlines in a match. #70

Closed myyellowshoe closed 3 years ago

myyellowshoe commented 4 years ago

I'm running into an issue where I'd love to select and loop over each line after a specific match: For example.

Text I'm parsing

Order:120983
Status: Error

ORDER LEVEL ERROR(S):
      ERRORCODE1: BAD THINGS HAPPENED
Heres a new line with more details about this bad error and things.
Heres another new line about the error.
      ERRORCODE2: MORE BAD THINGS HAPPENED
Heres a new line with more details about this bad error and things.
Heres another new line about the error.

-------------------

Template

Value ERROR_CODE (\w+)
Value ERROR_MESSAGE (.*)

Start
  ^ORDER LEVEL ERROR\(S\): -> ErrorList

ErrorList
  ^\s{6}${ERROR_CODE}:${ERROR_MESSAGE} -> Record

Results

ERROR_CODE | ERROR_MESSAGE
ERRORCODE1 | BAD THINGS HAPPENED
ERRORCODE2 | MORE BAD THINGS HAPPENED

As you can see I can get the first line just fine, but I'm struggling to figure out how to snag the next lines referring to that error. Anything I've tried seams to grab too much, and will start grabbing things in the next error code.

Essentially how would i get all new lines before the next error code? Thanks for any insight or thoughts!

harro commented 4 years ago

Something like this should do the trick

Value Required ERROR_CODE (\w+)
Value ERROR_MESSAGE (.*)
Value List ERROR_BODY (.*)

Start
  ^${ERROR_BODY}
  ^\s{6} -> Continue.Record
  ^\s{6}${ERROR_CODE}:${ERROR_MESSAGE}
myyellowshoe commented 4 years ago

Cool, i'll give this a shot and let you know how it works! Appreciate the response!