textFSM Collect values into list using the same Value variable

162 Views Asked by At

I'm new to textFSM. Trying to use the same value to find and save multiple instances of a value matches across multiple lines of input text. I have tried a number of different constructs but none provides the desired output.

Input

set=

 1. text1      2. text2         3. text3

 4. text4                       5. text5 
    
 6. text6

Template file:

Value List alm (\S+)

Start

^\s*\d+.\s+${alm} -> Continue.Record

Actual Result: [text1, text4, text6]

Desired Result: [text1, text2, text3, text4, text5, text6]

Any pointers on how to achieve the desired results?

1

There are 1 best solutions below

0
Marijn On

If you know the maximum number of entries per line, then you can add a separate regex for each entry. Note that you should not use Record as that finishes the list and starts a new row in the result table.

Template, adapted from https://github.com/google/textfsm/issues/11#issuecomment-273649451:

Value List alm (\S+)

Start
  ^\s*\d+.\s+${alm} -> Continue
  ^\s*\d+.\s+\S+\s*\d+.\s+${alm} -> Continue
  ^\s*\d+.\s+\S+\s*\d+.\s+\S+\s*\d+.\s+${alm}

Python code:

import textfsm
raw_data = """ 1. text1      2. text2         3. text3
 
 4. text4                       5. text5 
    
 6. text6"""
template = open("templatefile")
re_table = textfsm.TextFSM(template)
template.close()
print(re_table.ParseText(raw_data))

Result:

[[['text1', 'text2', 'text3', 'text4', 'text5', 'text6']]]

Of course this is not very general. There are proposals to build repeated matches per line into TextFSM, but these are not implemented yet. More information: Issue 11, Issue 18, Issue 69.