http://www.perlmonks.org?node_id=922446


in reply to Parsing text file to CSV

This is a shot at trying to produce this suggested output

#!/usr/bin/perl use warnings; use strict; my $sep = "----------"; my $post_sep = 0; my $text_count = 1; while(<DATA>) { chomp; if ( $_ =~ /Important Text: (\d+)/ ) { $text_count = $1; } if ( $_ =~ /^$sep/ ) { $post_sep = 1; next; } if ( $post_sep ) { my @field = split " ", $_; printf "%d,%s,%s,%s,%s,%s,%s\n", $text_count,$field[0],$field[1],$field[2],$field[3],$field[4], +$field[5]; } } __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 looks like:

C:\Code>perl report_parse.pl 1,TEST,TT,TT00,TT00.1,NO,xxxx 1,TEST,TT,TT00,**TT00.2,YES,XXXXXX 1,TEST,TT,TT00,**TT00.3,YES,XXX 1,TEST,TT,TT01,TT01.1,NO,XXXXXXXXXXX 1,TEST,TT,**TT02,TT02.1,YES,XXXXX C:\Code>

Replies are listed 'Best First'.
Re^2: Parsing text file to CSV
by apok69 (Initiate) on Aug 25, 2011 at 22:49 UTC
    Thanks I'm going to try both and see what happens.