我有类似的配置文件:
define host{
host_name xxxxxxxx1748
use windows-server
alias Comet
hostgroups +bu-automotiveprd,screen-automotiveprd2
address XXX.XXX.XXX.XXX
}
define host{
host_name yyyyyyyyy991
use aix-server
alias
hostgroups +bu-automotiveprd,screen-automotiveprd2
address YYY.YYY.YYY.YYY
}
我想搜索host_name,如果匹配,输出应如下所示:
示例-假设我要搜索yyyyyyyyy991
主机。
然后输出必须如下所示:
define host{
host_name yyyyyyyyy991
use aix-server
alias
hostgroups +bu-automotiveprd,screen-automotiveprd2
address YYY.YYY.YYY.YYY
}
尝试这个,
perl -0777 -ne '/([^\n]*{[^}]*(yyyyyyyyy991)[^}]*})/ && print "$1\n"' file
解释:
perl -0777
运行perl并将整个文件打包为一行。-ne
对于每一行(n
),执行e
以下命令()。/(pattern)/ && print "$1\n"
匹配正则表达式模式,并将其添加为第一个匹配组($1
),然后进行打印。模式:
[^\n]*{ ... }
匹配从行首(包括“ {”到下一个“}”)中的所有内容...
存在[^}]*(yyyyyyyyy991)[^}]*
:
}
yyyyyyyyy991
某个地方。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句