2014-10-20 64 views
0

我想在行中找到搜索字符串時返回整行。找到搜索字符串時返回整行

當我打印時,我得到了大部分我需要的東西,但是我只有10個字符。我得到了該行和字符串的開頭,然後約10個字符,但沒有更多。

這裏是我的代碼(在此先感謝):

use strict; 
use warnings; 

my $calls_dir = "Ask/"; 
opendir(my $search_dir, $calls_dir) or die "$!\n"; 
my @files = grep /\.txt$/i, readdir $search_dir; 
closedir $search_dir; 
print "Got ", scalar @files, " files\n"; 

#my %seen =(); 
foreach my $file (@files) { 
    my %seen =(); 
    my $current_file = $calls_dir . $file; 
    open my $FILE, '<', $current_file or die "$file: $!\n"; 


    while (<$FILE>) { 
     chomp; 
     if (/^*(.*)Contact\s*(.*)\r?$/i) { 
      $seen{$1} = 1; 

      foreach my $addr (sort keys %seen) { 

       print "\n"; 
       print $file; 
       print "\n"; 
       print "[$addr]\n"; 
       print "\n"; 
       print "\n"; 
      } 
     } 
    } 
    close $FILE; 
} 
+0

後一些輸入和輸出部分如果 – vks 2014-10-20 11:14:27

+1

你指的'整行/^*(。*)Contact \ s *(。*)\ r?$/i'作爲匹配的模式,'$ _'將包含整行 - 即做一個'print「$ _ \ n」; '在if語句之後 – arco444 2014-10-20 11:22:50

+0

非常感謝! – tlialin 2014-10-20 18:39:27

回答