#!/usr/bin/perl use strict; use warnings; undef $/; my @blocks = map { [ split /\n/ ] } ( split /(?=ITEM)/, ( map { s/#.*\n//; $_ } )[0] ); print "$_\n" for @{ $blocks[1] }; # print block no. 2 __DATA__ #This is the ITEM file ITEM NO:1 [aaa] 111 [bbb] 222 [ccc] 333 ITEM NO:2 [ddd] 444 [eee] 555 [fff] 666 ITEM NO:3