简体   繁体   English

在 Perl 中,如何从变电站的外部命令 append 与 output 进行正则表达式匹配?

[英]In Perl, how can I append a regex match with output from an external command in a substation?

Let's say I have a list of IPs that looks like this:假设我有一个如下所示的 IP 列表:

10.2.3.4 10.2.3.4
10.5.3.2 10.5.3.2
10.5.3.1 10.5.3.1
... ...

I know about the 'e' option in regex, which does an eval against the replacement string.我知道正则表达式中的“e”选项,它对替换字符串进行评估。 I just wanted to do a straight up replacement, I could do this:我只是想做一个直接的替换,我可以这样做:

s/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/`{$1}dig -x $1 +short`/ge;   

But, what I really want to do is append the host name to regex match like this:但是,我真正想做的是 append 主机名与正则表达式匹配,如下所示:

10.2.3.4 website.example.com 10.2.3.4 网站.example.com
10.5.3.2 dc01.example.com 10.5.3.2 dc01.example.com
10.5.3.1 dc02.example.com 10.5.3.1 dc02.example.com
... ...

If you all you need to do is append, you don't need a regex at all.如果您只需要 append,则根本不需要正则表达式。 Just do:做就是了:

chomp($_ .= '  ' . qx(dig -x $_ +short)) for @list;

I'm not sure what the {$1} is for in your example, though.不过,我不确定{$1}在您的示例中是做什么用的。

s/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/$1.`{$1}dig -x $1 +short`/ge;

Try this, it works for me:试试这个,它对我有用:

s/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/"$1 ". `dig -x $1 +short`/ge;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM