2014-02-20 63 views
0

奇怪的是我,但在下面的代碼沒有我得到任何錯誤,也沒有導致:(無法從文件打印線

use strict; 
    use warnings; 
    use File::Find; 

    my $path = $ARGV[0]; 
    find({ wanted => \&GetappropriateFile }, $path); 
    my @all_file; 
    sub GetappropriateFile 
    { 
     my $file = $_; 

     #my @all_file; 
    # print "$file\n"; 
     if (-f and /traces[_d+]/) 
     { 
     print "$file\n"; 
     # push(@all_file,$file); 
     open(FH, "<", $file) or die "cannot open file:$!\n"; 
      while(my $line = <FH>){ 
      print "$line\n"; 
      $line =~ /Cmd line: com.android.phone/g; 
      #print "$line\n"; 
      push(@all_file,$file); 
      last; 
      #print "$file\n"; 
     } 
    close(FH); 
    } 

    } 

至REGx下面的文本文件我用 - > /跡線[_d +]/

traces_com.android.phone_01-22-2014_01-15-54 traces_01-22-2014_06-24-25 traces_com.skype.raider_01-22-2014_01-15-54 traces_com。 android.mms_01-22-2014_01-15-54

回答

3

你的正則表達式匹配的是文件一樣:

traces_ 
tracesd 
traces+ 

這是你想要的嗎?

我猜你的意思是:

/traces(_\d+)?/ 

匹配:

traces 
traces_1 
traces_015 
traces_8675309 

...但我真的不知道自己想要什麼。

+0

我試圖告訴,當我試圖讀取文件沒有輸入輸出 – Maverick

+0

這行'print'$ file \ n「;'實際上產生輸出嗎?它是找到文件,但沒有閱讀它們? –

+0

如果是這樣的話,你可能想打開'$ File :: Find :: name'而不是'$ file'或'$ _',這將是沒有路徑的文件名。 –

1

如果文件以任何形式的擴展名結尾,如.txt或.tbl。此代碼不會工作,因爲跟蹤_ [數字] .txt不會與regx /痕跡[_d +] /找到。如果您正在使用Windows,那麼您可能看不到文件的擴展名。 我建議你使用下面的模式/traces[_d+].txt/或類似的東西。

另一種可能性是您嘗試打開的文件爲空。