我需要幫助拿出,作爲 SORT命令執行相同功能的工作perl腳本:sort –t’;’ –k1,1 File1.txt File2.txt File2.txt | uniq –u
比較數據,並打印出獨特的發現
我有兩個文件由分號分隔。我需要僅基於第一列(數字)fror File2.txt(差異而非)的唯一性提取出唯一行,其他列2和列3在此期間不相關。
FILE1.TXT(主文件)
123;winter;season
456;fall;season
789;autumn;season
321;summer;season
654;dry;weather
987;cold;weather
FILE2.TXT
123;winter;season
456;fall;season
789;autumn;season
321;summer;season
369;march;month
147;september;month
預期輸出(369 & 147不在FILE1.TXT)
369;march;month
147;september;month
迄今我寫了,但它打印出兩個文件;
#!/usr/bin/perl
# create names lookup table from first file
open(DATA, "<File1.txt") or die "Couldn't open file File1.txt, $!";
my %names;
while (<DATA>) {
(my @data)= split /;/, $_;
$names{$data} = 1;
last if eof;
}
# scan second file
open(DATA2, "<File2.txt") or die "Couldn't open file File2.txt, $!";
while (<DATA2>) {
print if /^(\d+)/ && not $data[0];
}
}
我仍然很難理解數組和哈希值。任何幫助改善我的代碼將不勝感激。請添加評論或指出我有任何錯誤...提前致謝。,