2016-03-04 38 views
0

我有兩個TXT文件:Perl來找到無與倫比的領域在兩個文件,一個文件

fileA.txt(製表符作爲分隔符)

field1 field2 
A  value1 
A  value2 
A  value3 
B  value112 
C  value33 
D  value11 
E  value3 
B  value23 
E  value5 

fileB.txt(製表符作爲分隔符)

field1 field2 
A  value1 
A  value3 
M  value9 
B  value5 
C  value33 

我希望腳本報告:

  1. 在fileB.t XT,如果兩個FIELD1具有不同FIELD2
    報告:甲value1和甲值3
  2. 在fileB.txt,如果FIELD2具有比FIELD2在fileA.txt對應於相同FIELD1
    報告不同值:B值5

所以腳本的輸出應該是:

A  value1 
A  value3 
B  value5 

我的腳本來完成#2:

#!/usr/bin/perl -w 

my $fileA="/tmp/fileA.txt"; 
my $fileB="/tmp/fileB.txt"; 
my @orifields; 
my @tmp; 
my @unmatched; 

open(FHD, "$fileB") || die "Unable to open $fileB: $!\n"; 
@orifields = <FHD>; 
close(FHD); 
chomp(@orifields); 

open(FHD, "$fileA") || die "Unable to open $fileA: $!\n"; 
@tmp = <FHD>; 
close(FHD); 
chomp(@tmp); 
foreach my $line (@tmp){ 
    print("Each line in fileA: $line\n"); 
} 

foreach my $line (@orifields) { 
    my ($field1, $field2) = split(/\t/, $line); 
    print("Field1 is: $field1, Field2 is: $field2\n"); 

    if (! grep(/$line/, @tmp)) { 
     if (grep(/$field1/,@tmp)) { 
     push(@unmatched,"$line"); 
     } 
    } 
} 

print("Unmatched: @unmatched\n"); 

是否有任何不錯的方法來實現兩個腳本沒有重複的變量?在此先感謝,

+0

你的標題太混亂了。你能否讓它更具可讀性 – Nagaraju

回答

2

使用散列記住文件的內容:

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

my %hash_a; 
open my $FA, '<', 'fileA.txt' or die $!; 
while (<$FA>) { 
    chomp; 
    my ($f1, $f2) = split /\t/; 
    undef $hash_a{$f1}{$f2}; 
} 


my %hash_b; 
open my $FB, '<', 'fileB.txt' or die $!; 
while (<$FB>) { 
    chomp; 
    my ($f1, $f2) = split /\t/; 
    push @{ $hash_b{$f1} }, $f2; 

    if (exists $hash_a{$f1} && ! exists $hash_a{$f1}{$f2}) { 
     print "#2: $f1 $f2\n"; 
    } 
} 

for my $key (grep @{ $hash_b{$_} } > 1, keys %hash_b) { 
    print join(' ', "#1: $key", @{ $hash_b{$key} }), "\n"; 
} 
+0

輝煌的腳本! – dellair