2010-11-17 60 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; 
相關問題