Match the consecutive lines in the file.

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} 

The code above applies to the temp.txt file. Another tempo.pl file has a perl scalar variable $ pattern. The value of $ 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} 

I want to write a block of code in perl or sed or awk or regex that returns me the template name ie template: perm_subcluster If the value of $ pattern matches the block of lines in temp.txt.

+4
source share
3 answers

Description

You will need to modify the β€œwhat I'm looking for” block to contain all the same leading spaces that exist in the target data.

Your text search will need to be inserted into this expression between the \Q ... \E tags. Then the expression will find the name of the template for the selected block of text, the name will be placed in 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 

Input text

 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} 

Matches

 [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 
+4
source

Assuming a Unix-style line ending:

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

With awk, for example, when searching for ymsagg-08 you can try:

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

Source: https://habr.com/ru/post/1488734/


All Articles