2013-05-31 71 views
-1

我需要比較兩個文件。在第一個文件中,我有一些ID對,在第二個文件中,我有用兩種樣式(每列一個)編寫的ID列表。他們是這樣的:比較文件和只寫匹配模式,perl或awk?

文件1

​​

文件2

IDnew_1 IDold_1 
IDnew_2 IDold_2 
IDnew_7 IDold_7 
IDnew_8 IDold_8 

我想獲得一個像這樣的輸出:

IDold_1 IDold_2 
IDold_7 IDold_8 

Pratically我需要「翻譯」文件1「中的舊式ID在文件二中。 我試過的東西在perl,但我不能在文件中有兩列工作2 我的Perl代碼如下所示:

$file_GS = "file1.txt"; 
$file_orto = "file2.txt"; 
open (HAN, "< $file_orto") || die "Impossible open input orto"; 
@r = <HAN>; 
close (HAN); 
open (GAS, "< $file_GS") || die "Immposible open GS file"; 
@p = <GAS>; 
close (GAS); 

for ($i=0; $i<=$#r; $i++){ 
chomp ($r[$i]); 
@orto = split (/\t/, $r[$i]); 
$old = $orto[0]; 
$new = $orto[1]; 

for ($l=0; $l<=$#p; $l++){ 
chomp ($p[$l]); 
@v = split (/\t/, $p[$l]); 
$gs1 = $v[0]; 
$gs2 = $v[1]; 

if ($gs1 eq pf_old){ 
print "$pb\n"; 
} 
} 
} 

此代碼只寫一列和輸出如下所示:

IDold_1 
IDold_7 

....我怎樣才能使它的工作給我輸出兩列? 建議?
謝謝!

+2

請向我們展示您的Perl代碼。輸出中有 – simbabque

+0

沒有來自file1的數據? –

+0

@mpapec在輸出中我有ID相當於文件1中的ID(舊ID中的新ID「翻譯」)。 – Gabelins

回答

0

首先,散列「翻譯」文件。然後,只需打印找到兩個ID的翻譯的那些行。

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

my $file_gs = 'file1.txt'; 
my $file_orto = 'file2.txt'; 

my %translate; 
open my $ORTO, '<', $file_orto or die $!; 
while (<$ORTO>) { 
    my ($new, $old) = split; 
    die "Duplicate $old" if exists $translate{$old}; 
    $translate{$new} = $old; 
} 

open my $GS, '<', $file_gs or die $!; 
while (<$GS>) { 
    my @ids = grep defined, map $translate{$_}, split; 
    print "@ids\n" if 2 == @ids; 
} 
+0

它完美的工作!謝謝你! – Gabelins

2

你必須給出一個更好的解釋你想要的。

我的假設是,你想使用文件#2作爲查找表。也就是說,您在文件#1中有一個新ID,並且您需要將其轉換爲文件#2中找到的舊ID。它是否正確?如果文件#1中的行在列#1中具有可轉換ID,但不在列#2中,該怎麼辦?你想要什麼?

在這種情況下,您需要首先讀入文件#2,並將新ID(密鑰)轉換爲舊ID(數據)。

#! /usr/bin/env perl 

use strict; 
use warnings; 
use feature qw(say); 
use autodie; 

use constant { 
    FILE_1 => "file1.txt", 
    FILE_2 => "file2.txt", 
}; 

# Read in File 2 and create a look up table 
open my $file2_fh, "<", FILE_2; 
my %lookup_table; 

while (my $line = <$file2_fh>) { 
    chomp $line; 
    my ($new_id, $old_id) = split /\s+/, $line; 
    $lookup_table{ $new_id } = $old_id; 
} 
close $file2_fh; 

現在您已經有了查找表,您可以輕鬆地將新ID轉換爲舊ID。讓我們通過文件#1

open my $file1_fh, "<", FILE_1; 
while (my $line = <$file1_fh>) { 
    chomp $line; 
    my ($new_id_1, $new_id_2) = split /\s+/, $line; 

    my ($old_id_1, $old_id_2); 

    if (exists $lookup_table{ $new_id_1 }) { 
     $old_id_1 = $lookup_table{ $new_id_1 }; 
    } 

    if (exists $lookup_table{ $new_id_2 }) { 
     $old_id_2 = $lookup_table{ $new_id_2 }; 
    } 

    # Now you've got to decide what to do here... 

    # First column is defined and second column isn't 
    if (defined $old_id_1 and not defined $old_id_2) { 
     say "Here be dragons..."; 
    } 
    # Second column is defined and first column isn't 
    elsif (not defined $old_id_1 and defined $old_id_2) { 
     say "Here be dragons..."; 
    } 
    # Both columns are defined 
    elsif (defined $old_id_1 and defined $old_id_2) { 
     say "$old_id_1 $old_id_2"; 
    } 
} 
close $file1_fh; 
+0

我認爲你的代碼很棒,但對我來說很難。我的編程太糟糕了,無法正確管理它...我嘗試了它,但是我收到了很多我無法理解的錯誤... :( – Gabelins

+0

我用你給的數據測試了這個程序。這是我使用的直接剪切和粘貼。 –