http://qs1969.pair.com?node_id=1078409


in reply to Parsing a clunky file

Based on the example, it looks like the lines you want are at the end of a blank-line-delimited record, so it would probably be easiest (and maybe even most reliable), if you read the file in "paragraph mode", and pop lines off the end of each record until you hit one that consists of just dashes. Something like this (updated to rearrange the conditions for the while loop, which could make it more reliable):
#!/usr/bin/perl use strict; use warnings; $/ = ""; # set input record separator to paragraph mode my $recid = 0; while (<DATA>) { my @lines = split /\n/; my @wanted = (); while ( @lines and $lines[-1] !~ /^-{10}/ ) { unshift @wanted, pop @lines; } if ( @wanted ) { unshift @wanted, sprintf( "Record # %d", ++$recid ); print join( "\n", @wanted ), "\n"; } } __DATA__ IFDI_PXJPB_ID MJ_IFDI_CJYSP_HYJP_ +C -------------------------------------------------- ------------------- +- CJYSP_MP + ---------------------------------------------------------------------- +---------- CJYSP_SUI_MP + ---------------------------------------------------------------------- +---------- CJYSP_ID CJYSP_ID_FJIP_CD + PFB -------------------------------------------------- ------------------- +- --- XXXXXX99954499999-99999999_94334633_SPJS_334 XXXX + KHYSSPFKBUPP MFKPBIFJS FF IFC + KHYSSPFKBUPP MFKPBIFJS FF, IFC + SKS_3449436933_36559337_994 IF + US + FJIP_IFKBJ_ID MJ_FJIP_CJYSP_HYJP_ +C -------------------------------------------------- ------------------- +- CJYSP_MP + ---------------------------------------------------------------------- +---------- CJYSP_SUI_MP + ---------------------------------------------------------------------- +---------- CJYSP_ID CJYSP_ID_FJIP_CD + PFB -------------------------------------------------- ------------------- +- --- SKSJCK39954494444-36559366_94334633_FJPX_994 XXXX + KKP MFIFCY F/P + KKP-MFIFCY F/P + SKS_3447994645_36559366_994 IF + US + FJIP_IFKBJ_ID MJ_FJIP_CJYSP_HYJP_ +C -------------------------------------------------- ------------------- +- CJYSP_MP + ---------------------------------------------------------------------- +---------- CJYSP_SUI_MP + ---------------------------------------------------------------------- +---------- CJYSP_ID CJYSP_ID_FJIP_CD + PFB -------------------------------------------------- ------------------- +- --- SKSF43399543F9P49-36563753_94334633_FJPX_994 YBS + KDK PPC CYBP. YF CFFFDF + KDK CYBP YF FMPBICF PFYBYJJ + SKS_3443656944_36563753_994 IF + US