Perl(比较两个文件)仅在主文件上打印出差异

时间:2015-02-03 21:32:47

标签: perl

我有一个需要比较的两个文件,并打印出第一个密钥文件中的差异。文件一是包含客户电话号码的密钥文件,文件字母表示类型是C-CELLPHONE或H-HOMEPHONE。第一个字母无关紧要。

C2817771111 MARY LEE
C8328883333 JOSEPH NGO
C2012122111 PETER LANDRY
C2123461111 PETER JAMES
C2531861212 DAMARIS PAUL
H8329195253 TIMOTHY HUE

第二个文件有更多列但是文件有两列是相同的

C8328883333  JOSEPH NGO    ADDRESS CITY STATE COUNTRY
C2012122111 PETER LANDRY ADDRESS CITY STATE  COUNTRY
C2123461111 PETER JAMES   ADDRESS CITY STATE  COUNTRY

#! /usr/bin/env perl
my %custphonehash = ();
$cellphone = "<cellphone.dat";
open (HOMEPHONES,"<homephone.txt");
open(CELLPHONES,$cellphone);
open(DELTA_RECORDS,">delta_phones.txt");
  while(<CELLPHONES>){
   chomp($cellphone =  $_);
   $custouthash{$cellphone} = substr($cellphone,1,10);
    }
    while(<HOMEPHONES>) {
     chomp($line =  $_);
    $phone_no = ($line,1,10)  

     if ( exists $custphonehash{$phone_no} 
     {
     print DELTA_RECORDS "$_\n" ;

     }

      }
   close CELLPHONES;
   close HOMEPHONES;
   close DELTA_RECORDS;*

输出应该是:

C2531861212 DAMARIS PAUL
H8329195253 TIMOTHY HUE

1 个答案:

答案 0 :(得分:0)

我会创建第二个文件中项目的哈希表,然后你可以迭代第一个文件并检查该数字是否存在,以及名称是否与数字匹配。

鉴于FILE1看起来像这样

C2817771111 MARY LEE
C8328883333 JOSEPH NGO
C2012122111 PETER LANDRY
C2123461111 PETER JAMES
C2531861212 DAMARIS PAUL
H8329195253 TIMOTHY HUE

和FILE2看起来像这样

C8328883333  JOSEPH NGO    ADDRESS CITY STATE COUNTRY
C2012122111 PETER LANDRY ADDRESS CITY STATE  COUNTRY
C2123461111 PETER JAMES   ADDRESS CITY STATE  COUNTRY

和脚本看起来像这样

use strict;
use warnings;

my $regex = qr/[CH](\d+) +(\w+ +\w+) *.*/;
my %hash;
while (<FILE2>) {
    my ($num, $name) = $_ =~ $regex;
    $hash{$num} = "$name";
}

while (<FILE1>) {
    my ($num, $name) = $_ =~ $regex;
    print unless (exists $hash{$num} and $hash{$num} = $name)
}

我得到以下输出

C2817771111 MARY LEE
C2531861212 DAMARIS PAUL
H8329195253 TIMOTHY HUE

养成始终使用strictwarnings pragma的习惯,因为它们将有助于正确调整代码范围并获取错误。

另外,打开文件的当前习惯用法是使用词法文件句柄,有三个参数,如此......

open my $homephone, '<', 'homephone.txt';
while (<$homephone>) {
    # do stuff
}
close $homephone