如何使用textfsm解析多行文本?

时间:2017-03-28 17:30:23

标签: text-parsing python-textfsm

我理解TextFSM是一种解析文本文件的好方法,但是,我看到它可以通过单行解析数据,我的问题是如何解析遍布多行的文本。

    <Page>


CUSIP No. 123456                  13G                   Page 2 of 10 Pages
-----------------------------------------------------------------------------
     (1)    NAMES OF REPORTING PERSONS

            ABC Ltd.

-----------------------------------------------------------------------------
     (2)    CHECK THE APPROPRIATE BOX IF A MEMBER OF A GROUP
                                                               (a)  [ ]
                                                               (b)  [X]
--------------------------------------------------------------------------------
     (3)    SEC USE ONLY
--------------------------------------------------------------------------------
     (4)    CITIZENSHIP OR PLACE OF ORGANIZATION

            Bruny Islands
--------------------------------------------------------------------------------
NUMBER OF      (5)   SOLE VOTING POWER
                     0
SHARES         -----------------------------------------------------------------

BENEFICIALLY   (6)   SHARED VOTING POWER

1,025,824 shares of Common Stock


OWNED BY       --------------------------------------------------------------

EACH           (7)   SOLE DISPOSITIVE POWER
                     0
REPORTING      --------------------------------------------------------------

PERSON WITH:   (8)   SHARED DISPOSITIVE POWER

1,025,824 shares of Common Stock


-----------------------------------------------------------------------------
     (9)    AGGREGATE AMOUNT BENEFICIALLY OWNED BY EACH REPORTING PERSON

1,025,824 shares of Common Stock


-----------------------------------------------------------------------------
     (10)   CHECK BOX IF THE AGGREGATE AMOUNT
            IN ROW (9) EXCLUDES CERTAIN SHARES
                                                                          [ ]
-----------------------------------------------------------------------------
     (11)   PERCENT OF CLASS REPRESENTED
            BY AMOUNT IN ROW (9)
            4.15%
-----------------------------------------------------------------------------
     (12)   TYPE OF REPORTING PERSON
            CO
-----------------------------------------------------------------------------

在上面的文字中,我想解析报告人和公民身份或组织地点的名称,这些名称不在一行中。解决这个问题的最佳方法是什么?

3 个答案:

答案 0 :(得分:3)

您可以使用TextFSM状态转换执行此操作。

此模板可满足您的需求:

Value REPORTING_PERSONS (\S+[\S ]+)
Value CITIZENSHIP (\S+[\S ]+)

Start
  ^.+NAMES OF REPORTING PERSONS -> Person
  ^.+CITIZENSHIP OR PLACE OF ORGANIZATION -> Citizenship
  ^ +NUMBER OF -> Record

Person
  ^ +${REPORTING_PERSONS}
  ^-+ -> Start

Citizenship
  ^ +${CITIZENSHIP}
  ^-+ -> Start

结果:

REPORTING_PERSONS    CITIZENSHIP
-------------------  -------------
ABC Ltd.             Bruny Islands

在这里你可以看到几个例子: https://github.com/google/textfsm/wiki/Code-Lab

答案 1 :(得分:0)

Value REPORTING_PERSON (\S+[\S ]+)
Value CITIZENSHIP (\S+[\S ]+)

Start
  ^.+NAMES\s+OF\s+REPORTING\s+PERSONS -> Person
  ^.+CITIZENSHIP\s+OR\s+PLACE\s+OF\s+ORGANIZATION -> Citizenship
  ^ NUMBER OF -> Record

Person
  ^(\s+)${REPORTING_PERSON} -> Start

Citizenship
  ^\s+${CITIZENSHIP} -> Start

答案 2 :(得分:0)

这是一个冗长而复杂的代码行的示例,我不想为此提出一个特定的正则表达式。

LSBATCH: User input
/hps/nobackup2/production/metagenomics/assembly-pipeline/prod/venv/bin/python /hps/nobackup2/production/metagenomics/...  -p DRP000303  -r DRR000714

相反,我只匹配包含User input的标记行之后的完整行:

Value job_command (.*)

Start
  # match line after line containing "User input"
  ^.*User input -> JobCommand #
  # some more rules...

JobCommand
  ^${job_command} -> Start