#!/usr/bin/perl use strict; use warnings; undef $/; open (FILE, '<', 'sample.txt') or die "Could not open sample.txt: $!"; my $file = ; my ($printline) = $file =~ m/.*?(print from.*?closing).*/sg; print $printline; close (FILE) or die "Could not close sample.txt: $!";