2012-07-26 72 views
-1

我正在使用以下腳本比較兩個文件:UEDP35.txtBB_UEDP35.txt。如果我手動輸入輸入,這個腳本運行良好,但現在我需要更多的東西。如何將輸入文件作爲perl腳本中的循環?

在一個文件夾中,我有一些文件,如UEDP35.txt。例如:UEDP1.txt,UEDP2.txt,UEDP3.txt等。類似地,存在另一組文件,例如BB_UEDP35.txt,例如, BB_UEDP1.txt,BB_UEDP2.txt,BB_UEDP3.txt等。UEDP在兩個文件名中都很常見。

如果有一對匹配的UEDP號碼,那麼我想給這兩個文件作爲腳本的輸入。如果在比較它們時發現任何差異,則必須將它們寫入另一個新文件。這必須按照循環完成。

use warnings; 
use strict; 

open AIN, "<UEDP35.txt "; 
open BIN, "<BB_UEDP35.csv"; 

my %seen; 
while (<ain>) { 
    my $v = (split(/,/))[0]; 
    $seen{$v}++; 
} 

while (<bin>) { 
    my $v = (split)[0]; 
    print "$vn" if not $seen{substr($v, 0, 5)}; 
} 

close AIN; 
close BIN; 

ls -1 UEDP* | while read line; do f1=echo $line | cut -f1 -d'.' ; f2=ls -1 BB_UEDP* | grep $f1;f3=echo $line | cut -f1; ./test.sh $f3 $f2;done 

例子:我有兩個文件:A.txtB.txt。在文件A.txt中,第一個文件具有五位數的數字。在文件B.txt中給出了整個數字。如果文件A.txt的前五位與第二個文件B.txt不匹配,那麼我需要將B.txt中的數字打印到另一個文件中。

A.TXT

81270,UEDP35 

81274,UEDP35 

87562,UEDP35 

89537,UEDP35 

90050,UEDP35 

B.txt

8127047667 

8756209276 

9956176149 

8127463873 

8953713146 

9935805068 

這裏A.txtUEDP35.txtB.txtBB_UEDP35.txt

+1

1st off:post code code which compiles。第二:如果你唯一的問題是你有多個文件,形式爲'UEDP'後跟一個數字,那麼簡單的for(1..n)'循環有什麼問題? – pavel 2012-07-26 17:31:06

+0

@pavel。我沒有得到。我使用這樣的東西。 ls -1 UEDP * |而讀線;做f1 ='echo $ line | cut -f1 -d'。'; f2 ='ls -1 BB_UEDP * | grep $ f1'; f3 ='echo $ line | cut -f1'; ./test.sh $ f3 $ f2;完成 作爲輸入。 – Mike 2012-07-26 17:43:37

+0

再次,你的實際代碼是什麼?請把它寫在你的問題中,而不是在評論中(不可讀);你在評論中顯示的是一些sh-code,而不是perl ... – pavel 2012-07-26 17:47:19

回答

0
use strict; 
use warnings; 

for my $bb_uedp_file (<BB_UEDP*>) { 
    if ($bb_uedp_file =~ /BB_UEDP(\d+)/) { 
    my $uedp_file = "UEDP$1.txt"; 
    open AIN, "<$uedp_file" or die($uedp_file); 
    open BIN, "<$bb_uedp_file" or die($bb_uedp_file); 
    # ... rest of the script ... 
    close AIN; 
    close BIN; 
    } 
} 
相關問題