找到搜索字符串时返回整行

时间:2014-10-20 11:12:57

标签: regex string perl printing line

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

当我打印时,我得到了我需要的大部分内容,但我只缺少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;
}

1 个答案:

答案 0 :(得分:0)

$ _包含您正在寻找的整行