Assuming file.txt has only one sentence per line:
John Depp is a great guy.
He is very inteligent.
He can do anything.
Come and meet John Depp.
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 will be the first and fourth line.
I want to make it work for a file that has random line breaks, and not just one English sentence per line. I mean that 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 conclusion should be the first and fourth sentences.
Any ideas please?
source share