2016-06-10 171 views
0

如何匹配{和}之间的多行。确实我需要匹配下面的块perl正则表达式大括号

host customerPERA { 
     host-identifier option agent.circuit-id "x:y"; 
     fixed-address yyy.yyy.yyy.yyy; 
     option routers xxx.xxx.xxx.xxx; 
     option subnet-mask 255.255.255.0; 
} 

我必须从非常大的dhcpd.conf文件中删除该块。 在此先感谢!

P.S.我忘了,我需要的正则表达式为Perl ..

回答

0

我做到了:

perl -pe 'BEGIN{undef $/;} s/^host customerPERA[^}]*}//smg' /etc/dhcp/dhcpd.conf 

感谢所有

+0

如果在输入内发现任何大括号,那么怎么办?可能会不符合? – ssr1012

+0

@ ssr1012:我配置dhcpd服务器已经很长时间了,但是如果我正确记得,没有任何在主机定义中使用大括号的实例。但我可能是错的 – stevieb

0

的解决你的问题:

perl -p0i -e 's/host\s+customerPERA\s+\{[^}]+\}/host customerPERA {}/g' /etc/dhcp/dhcpd.conf 

它将取代 “主机customerPERA {}” 上的 “主机customerPERA {}”(空大括号)

+2

可能还需要/ s多行也 – xxfelixxx

+2

@xxfelixxx:/ s改变'.'行为的方式。正则表达式中没有'.'。 – choroba

+2

'/ m'改变'^'和'$'的行为方式。这里不需要。 – choroba

0

我能做到这样:

输入:

host customerPERA{ 
     host-identifier option agent.circuit-id "x:y"; 
     fixed-address yyy.yyy.yyy.yyy; 
     option routers xxx.xxx.xxx.xxx; 
     option subnet-mask 255.255.255.0; 
} 

如果这是找到的情况下精确匹配:

正则表达式:

$myRegex = qw/((?:[^{}]*(?:{(?:[^{}]*(?:{(?:[^{}]*(?:{[^{}]*})*[^{}]*)*})*[^{}]*)*})*[^{}]*)*)/; 

if($pattern=~m/host CustomerPERA\{$myRegex\}/gs) 
{ 
     #do it you statement here 
} 
0

你想与(?s)本地使用single line modifier。 这可以给你一个行代码来删除或编辑你想要的。

$string =~ s/host customerPERA \{(?s).+?\}//;