return entire line when search string is found

限于喜欢 提交于 2019-12-11 22:24:52

问题


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



来源:https://stackoverflow.com/questions/26464291/return-entire-line-when-search-string-is-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!