如何grep存储全文本文件和打印匹配行的变量

时间:2011-12-14 03:03:49

标签: perl

您好我一直在尝试执行一个代码,其中我使用变量$ logs来保存我的所有Linux日志。 现在我想要为模式grep变量并打印整行中包含模式的行。 我想要打印grep / pattern /的整行,并且必须打印包含图案的线条。 无论如何,这是我的代码。

my $logs = $ssh->exec("cat system_logs");
my $search = "pattern";

if(grep($search,$logs))
{
    # this is where i want to print the lines matched.
    # I want to print the whole line what command to use?
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:5)

试试这个:

foreach (grep(/$search/, split(/\n/, $logs))) {
    print $_."\n";
}
相关问题