找到搜索字符串时返回整行 [英] return entire line when search string is found

查看:113
本文介绍了找到搜索字符串时返回整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当我打印时,我得到了我所需要的大部分东西,但是我缺少大约10个字符.我得到了该行和字符串的开始,然后是大约10个字符,但仅此而已.

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;
}

推荐答案

$ _包含您要查找的整行

$_ contains the entire line you're looking for

这篇关于找到搜索字符串时返回整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆