return entire line when search string is found

2019-09-17 16:07发布

问题:

I want to return the whole line when I find the search string in the line.

I'm getting most of what I need when I print but I'm short about 10 characters. I get the start of the line and the string and about 10 characters after that but nothing more.

Here's my code (thanks in advance):

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:

$_ contains the entire line you're looking for