【问题标题】:Match consecutive lines in a file.匹配文件中的连续行。
【发布时间】:2013-06-28 17:24:03
【问题描述】:
template: perm_subcluster
   copy_cluster: yms_cfg_ref
   allocations:
   - type: cfgstore
     hosts:
     - {name: ymscfg-02.ops.bf1.yahoo.com, farm: east}
     - {name: ymscfg-02.ops.gq1.yahoo.com, farm: west}
   - type: aggregator
     hosts:
     - {name: ymsagg-08.ops.bf1.yahoo.com, farm: east}
     - {name: ymsagg-10.ops.gq1.yahoo.com, farm: west}
   - type: metricsdb
     hosts:
     - {name: ymsdb-11.ops.bf1.yahoo.com, farm: east}
     - {name: ymsdb-11.ops.gq1.yahoo.com, farm: west}

以上代码属于文件 temp.txt。 另一个文件 tempo.pl 有一个 perl 标量变量 $pattern。 $pattern 的值为:

- type: cfgstore
  hosts:
  - {name: ymscfg-02.ops.bf1.yahoo.com, farm: east}
  - {name: ymscfg-02.ops.gq1.yahoo.com, farm: west}
- type: aggregator
  hosts:
  - {name: ymsagg-08.ops.bf1.yahoo.com, farm: east}
  - {name: ymsagg-10.ops.gq1.yahoo.com, farm: west}
- type: metricsdb
  hosts:
  - {name: ymsdb-11.ops.bf1.yahoo.com, farm: east}
  - {name: ymsdb-11.ops.gq1.yahoo.com, farm: west}

我想用 perl 或 sed 或 awk 或正则表达式编写一段代码,它返回模板名称,即 模板:perm_subcluster 如果 $pattern 的值与 temp.txt 中的行块匹配。

【问题讨论】:

  • 你能确认数据实际上是以YAML格式存储的吗?然后,最好将数据解析为数据结构,并查看模板数据结构中包含模式的位置。或者它 必须 是正则表达式吗? (并非不可能,只是令人难以置信的错误)。
  • 什么是matches= equal~ matches
  • $patterntemp.txt 中的前导空格是否相等?或者至少 $patterntemp.txt 总是相差 3 个空格?并且该块是否总是在template: 之后恰好开始三行?
  • @amon - 是的,数据以 YAML 格式存储,但是 YAML 文件非常乱码,所以我决定不解析而是搜索模式来获取模板的名称。
  • @m.buettner - 是的,该块总是在模板之后恰好开始三行。此外,变量 $pattern 中的前导空格可以匹配文件中的前导空格,因为我正在构建变量 $pattern。

标签: regex perl sed awk grep


【解决方案1】:

说明

您必须修改“我正在搜索的内容”块以包含与目标数据中存在的所有相同的前导空格。

您的文本搜索需要插入到\Q...\E 标记之间的此表达式中。然后,表达式将为您选择的文本块找到模板名称,该名称将被放入 Capture Group 1。

^template:\s*(\S*).*?(?=^)(?:^\s+(?:(?!^).)*)*?^\Q   - type: cfgstore
     hosts:
     - {name: ymscfg-02.ops.bf1.yahoo.com, farm: east}
     - {name: ymscfg-02.ops.gq1.yahoo.com, farm: west}
   - type: aggregator
     hosts:
     - {name: ymsagg-08.ops.bf1.yahoo.com, farm: east}
     - {name: ymsagg-10.ops.gq1.yahoo.com, farm: west}
   - type: metricsdb
     hosts:
     - {name: ymsdb-11.ops.bf1.yahoo.com, farm: east}
     - {name: ymsdb-11.ops.gq1.yahoo.com, farm: west}\E

输入文字

template: perm_subcluster
   copy_cluster: yms_cfg_ref
   allocations:
   - type: cfgstore
     hosts:
     - {name: ymscfg-02.ops.bf1.yahoo.com, farm: east}
     - {name: ymscfg-02.ops.gq1.yahoo.com, farm: west}
   - type: aggregator
     hosts:
     - {name: ymsagg-08.ops.bf1.yahoo.com, farm: east}
     - {name: ymsagg-10.ops.gq1.yahoo.com, farm: west}
   - type: metricsdb
     hosts:
     - {name: ymsdb-11.ops.bf1.yahoo.com, farm: east}
     - {name: ymsdb-11.ops.gq1.yahoo.com, farm: west}
template: Not_me
   copy_cluster: yms_cfg_ref
   allocations:
   - type: cfgstore
     hosts:
     - {name: Fail_ymscfg-02.ops.bf1.yahoo.com, farm: east}
     - {name: Fail_ymscfg-02.ops.gq1.yahoo.com, farm: west}
   - type: aggregator
     hosts:
     - {name: ymsagg-08.ops.bf1.yahoo.com, farm: east}
     - {name: ymsagg-10.ops.gq1.yahoo.com, farm: west}
   - type: metricsdb
     hosts:
     - {name: ymsdb-11.ops.bf1.yahoo.com, farm: east}
     - {name: ymsdb-11.ops.gq1.yahoo.com, farm: west}

匹配项

[0] => template: perm_subcluster
   copy_cluster: yms_cfg_ref
   allocations:
   - type: cfgstore
     hosts:
     - {name: ymscfg-02.ops.bf1.yahoo.com, farm: east}
     - {name: ymscfg-02.ops.gq1.yahoo.com, farm: west}
   - type: aggregator
     hosts:
     - {name: ymsagg-08.ops.bf1.yahoo.com, farm: east}
     - {name: ymsagg-10.ops.gq1.yahoo.com, farm: west}
   - type: metricsdb
     hosts:
     - {name: ymsdb-11.ops.bf1.yahoo.com, farm: east}
     - {name: ymsdb-11.ops.gq1.yahoo.com, farm: west}
[1] => perm_subcluster

【讨论】:

    【解决方案2】:

    假设 Unix 风格的行尾:

    $temp_txt =~ /template:\s*(.*)\n(\s.*\n)*?\Q$pattern/;
    return $1;
    

    【讨论】:

      【解决方案3】:

      使用 awk,例如在寻找 ymsagg-08 时,您可以尝试:

      awk '$1=="template:"{t=$2} $0~s{print t}' s="ymsagg-08" file
      

      【讨论】:

        猜你喜欢
        • 2013-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-07
        • 2021-05-19
        • 1970-01-01
        • 2019-11-14
        • 1970-01-01
        相关资源
        最近更新 更多