Perl (Scripting) - Loop through a file until line match

by
Jeremy Canfield |
Updated: August 18 2020
| Perl (Scripting) articles
Let's say you have a file that contains fruit.
apple
banana
orange
pineapple
This markup will print lines each line in the file until a line containing "orange" is matched.
my $file = "/path/to/file.txt";
open(FH, "<", $file) or die "cannot open $file $! \n";
while (my $line = <FH>) {
print $line;
last if $line =~ /orange/i;
}
close FH;
Running this script will produce the following output.
apple
banana
orange
Did you find this article helpful?
If so, consider buying me a coffee over at