Assuming file.txt
has just one sentence per line as follows:
John Depp is a great guy. He is very inteligent. He can do anything. Come and meet John Depp.
The Perl code is as follows:-
open ( FILE, "file.txt" ) || die "can't open file!";
@lines = <FILE>;
close (FILE);
$string = "John Depp";
foreach $line (@lines) {
if ($line =~ $string) { print "$line"; }
}
The output is going to be first and fourth line.
I want to make it working for the file having random line breaks rather than one English sentence per line. I mean it should also work for the following:-
John Depp is a great guy. He is very intelligent. He can do anything. Come and meet John Depp.
The output should be first and fourth sentences.
Any ideas please?
More simple: if you assume "sentences" are separated by dots, then you can use that as field separator:
$/ = '.';
while(<>) {
print if (/John Depp/i);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With