#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $file = 'file.txt'; open(my $fh, "<", $file) or die "Can't open ".$file." error: $!"; while( defined( my $line = <$fh> ) ) { chomp $line; next if $line =~ /^\s*$/; # skip empty lines next if (index($line, 'second') != -1); say $line; } close $fh or warn "Can't close ".$file." error: $!"; __DATA__ This is the first line in the file. This is the second line in the file. This is the third line in the file. This is the forth line in the file. __OUTPUT__ $ perl test.pl This is the first line in the file. This is the third line in the file. This is the forth line in the file.