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

abh*_*uri 0 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?
}
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏.

sjs*_*sjs 5

试试这个:

foreach (grep(/$search/, split(/\n/, $logs))) {
    print $_."\n";
}
Run Code Online (Sandbox Code Playgroud)