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

4

3 回答 3

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
于 2013-06-28T18:38:06.407 回答
2

假设 Unix 风格的行尾:

$temp_txt =~ /template:\s*(.*)\n(\s.*\n)*?\Q$pattern/;
return $1;
于 2013-06-28T21:17:32.980 回答
0

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

awk '$1=="template:"{t=$2} $0~s{print t}' s="ymsagg-08" file
于 2013-06-30T10:00:46.420 回答