2014-02-18 12 views
0

我有一個perl腳本,用於管理將特定文件格式轉換爲可以稍後管理的csv文件的轉換。Perl - 通過在編寫之前檢查打開的文件中是否存在模式來防止重複

我需要這個腳本能夠防止產生重複行:

#get timetamp 
    if ((rindex $l,"ZZZZ,") > -1) { 
      (my $t1, my $t2, my $timestamptmp1, my $timestamptmp2) = split(",",$l); 
      $timestamp = $timestamptmp2." ".$timestamptmp1; 
    } 

    if (((rindex $l,"TOP,") > -1) && (length($timestamp) > 0)) { 
    (my @top) = split(",",$l); 
     my $aecrire = $SerialNumber.",".$hostnameT.",".$timestamp.",".$virtual_cpus.",".$logical_cpus.",".$smt_threads.",".$top[1]; 
     my $i = 3;########################################################################### 
     while ($i <= $#top) { 
     $aecrire = $aecrire.','.$top[$i]; 
      $i = $i + 1; 
     } 
     print (FIC2 $aecrire."\n"); 
    } 

我的源文件是FIC1和目標文件FIC2中,uniq的關鍵是$時間戳。

我希望腳本檢查$時間戳FIC1(這是在開始過程中打開)已經存在,如果它不被寫入FIC2排除線。 如果$ timestamp不存在,則按正常方式寫入。

目前,如果重新運行在一個已經進行文件中的腳本,每一行將由時間戳進行排序和複製。

我的目標是能夠定期在文件上運行此腳本而不復制事件。

我很新的Perl中,據我已經看到了這個應該簡單地用看到的,而內變量%來實現,但我還沒有實現它成功...

謝謝非常提前的任何幫助:-)

+0

在Linux上你?您可以將您的輸出傳送到uniq – Jeef

回答

1

你在描述什麼是散列。

你會在你的代碼,當你讀線定義的哈希

my %seen =(); 

然後 - 你決定寫這之前,你可以這樣做:

#Check the hash to see if we have seen this line before we write it out 

if ($seen{$aecrire} eq 1) { 
#Do nothing - skip the line 
} else { 
$seen{$aecrire} = 1; 
print (FIC2 $aecrire."\n"); 
} 

我沒有檢查這段代碼,但那是jist。

+0

在聲明散列(或任何類型的變量)時,沒有必要分配空列表(或任何值)。 '我見%'是你需要的。不過,用於提示散列的+1。 – TLP

+0

嗨, 我試圖添加條件,這幾乎工作,因爲它不再生成所有重複行,但它仍然會生成一個重複行每個執行...(最後一行) uniq鍵應該是時間戳,如果時間戳存在FIC1中,腳本不應該寫 我已經添加了你的代碼(我用($ seen {$ timestamp})替換($ seen {$ aecrire})替換了該條件下的打印行 並且我添加了:我見過%;在腳本的頂部(也試圖與=()看到我的%) 我在做什麼錯 此外,腳本輸出%以內在串EQ看到消息「使用未初始化值的..? 「 – Guilmxm

+0

我是不是100%確定,但如果你不打算檢查它是否等於1,你可能需要做(如果存在$看到{$ timestamp})...你是否也使用嚴格? – Jeef

0

我最終通過我的過程的最後添加以下代碼:

my (@final, %hash, $file) = ((),(), ""); 

foreach $file ($dstfile_CPU_ALL, $dstfile_MEM, $dstfile_VM, $dstfile_PROC, $dstfile_TOP) { 

     if (!open FILE, "+<$file") { 
       print "Nothing to dedup, '$file' $!\n"; 
       next; 
     } 

     while (<FILE>) { 
       if (not exists $hash{$_}) { 
         push @final, $_; 
         $hash{$_} = 1; 
       } 
     } 

     truncate FILE, 0; 
     seek FILE, 0, 0; 
     print FILE @final; 
     close FILE; 
     %hash = @final =(); 
} 
相關問題