Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Parsing text file to CSV

by thewebsi (Scribe)
on Sep 13, 2011 at 19:56 UTC ( [id://925762]=note: print w/replies, xml ) Need Help??


in reply to Parsing text file to CSV

Perl code can be remarkably compact if your requirements are loose (as they appear to be).

#!/usr/bin/perl use strict; my $important; while ( ( $_ = <DATA> ) !~ /^\-{10,}$/ ) { $important = $1 if /^Important Text: (\d+)$/i; } while ( <DATA> ) { print join ( ",", $important, split ( /\s+/ ) ) . "\n"; } __DATA__ Date 08/17/11 Report Page 1 Time 12:46 Important Text: 1 Misc Text: All Misc Text: Sec ** Indicates APPTEXT PLINE SCODE PCODE FID SEC unsec fcs ---------------------------------------------------------------------- +--------------------------- TEST TT TT00 TT00.1 NO xxxx TTD TEST TT TT00 **TT00.2 YES XXXXXX TEST TT TT00 **TT00.3 YES XXX TEST TT TT01 TT01.1 NO XXXXXXXXXXX TT TEST TT **TT02 TT02.1 YES XXXXX
Output:
1,TEST,TT,TT00,TT00.1,NO,xxxx,TTD 1,TEST,TT,TT00,**TT00.2,YES,XXXXXX 1,TEST,TT,TT00,**TT00.3,YES,XXX 1,TEST,TT,TT01,TT01.1,NO,XXXXXXXXXXX,TT 1,TEST,TT,**TT02,TT02.1,YES,XXXXX

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-18 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found