TextFSM逻辑 - 避免两次捕获相同的数据

时间:2017-10-19 14:35:43

标签: python regex

我试图从cisco RFGW上记录光学读数。我面临的问题是,由于数据的显示方式,我捕获物理接口两次,并在每次输入后追加我想要的数据。在我收集了信息之后,我当然可以修复它,但是我想在解析输出后避免大量数据操作。

原始文字:    HE-EQM-01#show interfaces transceiver detail | begin Transmit Power Transmit Power Threshold Threshold Threshold Threshold Port (dBm) (dBm) (dBm) (dBm) (dBm) --------- ----------------- ---------- --------- --------- --------- Te1/2 -3.0 1.6 -1.3 -7.3 -11.3 Te1/3 -17.3 1.6 -1.3 -7.3 -11.3 Te1/4 -40.0 1.6 -1.3 -7.3 -11.3 Te2/2 -3.1 1.6 -1.3 -7.3 -11.3 Te2/3 -40.0 1.6 -1.3 -7.3 -11.3 Te2/4 -40.0 1.6 -1.3 -7.3 -11.3     Optical High Alarm High Warn Low Warn Low Alarm Receive Power Threshold Threshold Threshold Threshold Port (dBm) (dBm) (dBm) (dBm) (dBm) ------- ----------------- ---------- --------- --------- --------- Te1/2 -40.0 1.9 -1.0 -9.9 -13.9 Te1/3 -2.6 1.9 -1.0 -9.9 -13.9 Te1/4 -2.9 1.9 -1.0 -9.9 -13.9 Te2/2 -3.0 1.9 -1.0 -9.9 -13.9 Te2/3 -2.6 1.9 -1.0 -9.9 -13.9

模板:

Value interface (\w+\d\/\d+)
Value tx (-*\d+\.\d+)
Value rx (-*\d+\.\d+)

Start
  ^\s*Transmit\s*Power.+
  ^\s*${interface}\s+${tx} -> Record

输出:

[['Te1/2', '-3.0', ''],
['Te1/3', '-17.3', ''],
['Te1/4', '-40.0', ''],
['Te2/2', '-3.1', ''],
['Te2/3', '-40.0', ''],
['Te2/4', '-40.0', ''],
['Te1/2', '-40.0', ''],
['Te1/3', '-2.6', ''],
['Te1/4', '-2.9', ''],
['Te2/2', '-3.0', ''],
['Te2/3', '-2.6', ''],
['Te2/4', '-2.4', '']]

最好:

[['Te1/2', '-3.0', '-40.0'],
['Te1/3', '-17.3', '-2.6'],
['Te1/4', '-40.0', '-2.9'],
['Te2/2', '-3.1', '-3.0'],
['Te2/3', '-40.0', '-2.6'],
['Te2/4', '-40.0', '-2.4'],

1 个答案:

答案 0 :(得分:0)

我分解了一下。你想要的是:

  1. 匹配界面:(\w+\d\/\d+)
  2. 在几个空格后,匹配发射功率:(-?\d\.\d+)(此处最好使用?,因为您不希望多次跟踪-
  3. 在下面的文本中再次查找相同的界面:(?=.*\1)
  4. 那一个接着是接收功率(与2相同。)
  5. 如果你全部组装,你会得到:

    (\w+\d+\/\d+)\s+(-?\d+\.\d+)(?=.+\1\s+(-?\d+\.\d+))
                                          ^
    Capturing in the lookahead, that way relevant data is captured, but not eaten by the engine, which would prevent following matches.
    

    请参阅demo