Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Parsing a clunky file

by graff (Chancellor)
on Mar 15, 2014 at 01:37 UTC ( [id://1078409]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1078409]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-24 05:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found