【发布时间】: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? -
$pattern和temp.txt中的前导空格是否相等?或者至少$pattern和temp.txt总是相差 3 个空格?并且该块是否总是在template:之后恰好开始三行? -
@amon - 是的,数据以 YAML 格式存储,但是 YAML 文件非常乱码,所以我决定不解析而是搜索模式来获取模板的名称。
-
@m.buettner - 是的,该块总是在模板之后恰好开始三行。此外,变量 $pattern 中的前导空格可以匹配文件中的前导空格,因为我正在构建变量 $pattern。