2010-11-17 62 views
2

我的IP地址列表,我不得不开始210.x.x.x所有有限合伙人转变为10.x.x.x如何用Perl替换文件中的特定IP?

例如:

210.10.10.217.170 ---->10.10.10.217.170

有没有在线的Perl正则表达式替代做到这一点?

我想有这种替代在Perl。

+0

你有你的输入文件的样本? – 2010-11-17 14:14:33

+0

这些都不是有效的IP地址。 IP地址有四个部分 - 这些值有五个。 – 2016-07-07 10:38:23

回答

2

你为什么不使用sed的呢?

sed -e 's/^210\./10./' yourfile.txt 

如果你真的想要一个perl脚本:

while (<>) { $_ =~ s/^210\./10./; print } 
+2

我怀疑你想在那里有一个/ g。 Perl的等同于战略经济对话'的perl -pe的/^210 \ ./ 10./” yourfile.txt' – ysth 2010-11-17 14:56:45

3
$ip =~ s/^210\./10./; 
1

你可以使用perl -pe遍历文件的行,做一个简单的替换:

perl -pe 's/^210\./10./' file 

或者就地修改文件:

perl -pi -e 's/^210\./10./' file 

perlruns///

+0

请与您的代码一起添加一些说明,谢谢:) – Will 2016-07-06 22:17:32

+0

@Wil:NP,查看更新。 – 2016-07-07 10:10:35

+0

太棒了,谢谢! – Will 2016-07-07 10:36:13

1
# Read the IP list file and store in an array 
$ipfile = 'ipfile.txt'; 
open(ipfile) or die "Can't open the file"; 
@iplist = <ipfile>; 
close(ipfile); 

# Substitute 210.x IPs and store all IPs into a new array 
foreach $_(@iplist) 
{ 
    s/^210\./10\./g; 
    push (@newip,$_); 
} 

# Print the new array 
print @newip; 
相关问题