如何提取IP地址和使用Perl的端口号,并比较其

如何提取IP地址和使用Perl的端口号,并比较其

问题描述:

我有一个包含字符串中的端口号和IP地址的文件。我要提取的IP地址,根据公式计算的端口号,并将其与文件中的端口号,并打印不匹配的人。如果IP地址是W.X.Y.Z,则端口号的公式为50000 + 200(Y)+ Z。 文本文件格式如下。如何提取IP地址和使用Perl的端口号,并比较其

exchangeA_5 = 53413; 239.189.17.13 7990

exchangeA_6 = 53415; 239.189.17.15 7990

exchangeA_e = 53470; 239.189.27.70 7990

exchangeA_5 = 53468; 239.189.27.68 7990

什么是做到这一点的最好方法是什么?

#!/usr/bin/perl 
use strict; 
use warnings; 

open(my $fh, '<', 'fileC') 
or die("Can't open fileC: $!\n"); 

while (<$fh>) { 
    chomp; 
    my ($key, $val) = split /=/; 
    #print "$key\n"; 
    #print "$val\n"; 

    my ($port, $ip) = split /[;]/, $val; 
    print "$port\n"; 
    print "$ip\n"; 

} 

快速和肮脏的:

perl -ne '($host, $port, @ip) = split /[=;.]/; print if $port != 50000+200*$ip[2]+$ip[3]' fileC 

当然,要改写成一个不错的节目:)

保罗

+6

'perl的-F \\ W此-nae'$ F [1] -50000- $ F [5] * 200- $ F [6] && print'' – ysth

open(my $fh, '<', 'fileC.txt'); 
while (<$fh>) { 
    chomp; 
    if (!/^$/){ 
    ($Host,$port, @IP) = split /[=;.]/; 
    print "Host:$Host, IP:", (join '.',@IP),", and Port:$port\n"; 
}}