Reputation: 1287
I am new to Perl. I want to find a string in some file and then i want the whole line which contains the string.
Upvotes: 5
Views: 7938
Reputation: 129
if ( !open(LOGFILE, "<myfile.log") )
{
print "ERROR: failed to open myfile.log\n";}
else {
while (<LOGFILE>){
if ($_ =~ /pattern/)
{ print "found\n";
break;
}
}
close (LOGFILE);
}
Upvotes: 2
Reputation: 27248
Loop, while there are lines to read from the file
2.1 Using regular expressions, check if the line matches the pattern: if ($line =~ /pattern/)
.
2.2 If yes, print the string
Close the file.
Upvotes: 9