我在這裏有兩個文件(文件1 &文件2)。我想從兩個文件中匹配如下所示的粗體名稱。不過,我需要以文件1格式打印這些不匹配的數據。我一直在嘗試下面的代碼,但它不是我想要的結果。如何在匹配後以文件1格式打印那些不匹配的數據?如何在比較兩個文件後打印不匹配的數據?
文件1
ID **alan135/xkr** $work(b05bfn00un0c3)/b05bfn00un0c3 ; #<= b05bfn00un0d0 Size:5848.270996
ID **John06/ext** $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
ID **lily099/poli** $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
ID **sam012/pp** $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
ID **lily099/poli** $wwrk(b05bfn00ld0p8)/b05bfn00ld0p8 ; #<= b05bfn00ld0s0 Size:INFINITY
ID **Steve9018** $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
文件2
Accept => **John06/ext** Max
Accept => **vivian788/ppr** Maxcap
Accept => **suzan645/pp** Min
Accept => **lily099/poli** Max
Accept => **Nick5670/uu** Max
Accept => **Anne309/pej** Min
代碼
my ($line1,$line2,@arr1,@arr2,@arr3,@emptyarr);
@arr1 = <FILE1>;
@arr2 = <FILE2>;
foreach $line2 (@arr2) {
if ($line2 =~ m/(.*)\s+(.*)\s+(.*)\s+(.*)/) {
@arr3 = @emptyarr;
my $cname2 = "$2";
push (@arr3, $cname2);
}
}
foreach $line2 (@arr3) {
foreach $line1 (@arr1) {
if ($line1 =~ m/(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)/) {
my $cname1 = "$2";
if ($cname1 ne $line3) {
print NL "$cname1\n";
}
}
}
}
預期的結果:
ID alan135/XKR $ WOR k(b05bfn00un0c3)/ b05bfn00un0c3; #< = b05bfn00un0d0大小:5848.270996
ID sam012/pp $ work(b05bfn00ld0p7)/ b05bfn00ld0p7; #< = b05bfn00ld0s0大小:INFINITY
ID Steve9018 $ work(b05bfn00ld0p7)/ b05bfn00ld0p7; #< = b05bfn00ld0s0大小:INFINITY
你會發現它更容易將代碼粘貼在(適當的縮進),然後使用「{}」按鈕在編輯器上對代碼進行格式化,而不是在每行代碼上使用「'」。 – 2013-07-17 03:20:41