匹配文件中的连续行。

时间:2013-06-28 17:24:03

标签: regex perl sed awk grep

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或regex中编写一个代码块,它返回模板名称,即 template:perm_subcluster 如果$ pattern的值与temp.txt中的行块匹配。

3 个答案:

答案 0 :(得分:4)

描述

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

您需要在\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

答案 1 :(得分:2)

假设Unix风格的行尾:

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

答案 2 :(得分:0)

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

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