#!/usr/bin/perl use strict; use warnings; my $file = 'log.dat'; $/ = "[end.message]\n"; my $search = 'test 1'; open my $fh, '<', $file or die "cannot open $file: $!"; while (<$fh>) { next unless /^\Q$search\E/; chomp; print; } close $fh;