#!/usr/bin/env perl -l use strict; use warnings; use autodie; my $re = qr{^([jEG].*)$}m; { local $/ = "\nh"; open my $fh, '<', 'pm_1055252_data.txt'; while (<$fh>) { print "*** h-block #$."; print $1 while /$re/g; } close $fh; }