abhiram potluri
abhiram potluri

Reputation: 45

How to grep a variable which stores a full text file and print matching lines

Hi I have been trying to execute a code where i used a variable $logs to save all my linux logs. Now i want to grep the variable for a pattern and print the whole line for the lines that have the pattern in them. I want to print whole line where i do grep /pattern/ and the lines that have pattern in them have to be printed. Anyways here is my code.

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

any help is greatly appreciated.

Upvotes: 0

Views: 2779

Answers (1)

sjs
sjs

Reputation: 9310

Try this:

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

Upvotes: 5

Related Questions