开发者

How do I substitute a particular IP in a file with Perl?

I have a list of IPs, I have to transform all the lPs starting with 210.x.x.x to 10.x.x.x

For exa开发者_运维技巧mple:

210.10.10.217.170 ----> 10.10.10.217.170

Is there any in-line Perl regular expression substitution to do that?

I would like to have this substitution in Perl.


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


why don't you use sed instead ?

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

If you really want a perl script :

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


# 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;


You could use perl -pe to iterate over the lines of the file and do a simple substitution:

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

Or to modify the file in-place:

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

See perlrun and s///.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜