use strict; use warnings; my( $company, $co_id ); my $item; local $, = ","; local $\ = "\n"; while () { next unless /\S/; # ignore blank lines next if defined $item && /^ *Total \Q$item\E /; next if defined $co_id && /^ *Total \Q$company:$co_id\E /; if ( /^ *(.+):(\S+) *$/ ) { ( $company, $co_id ) = ( $1, $2 ); } elsif ( /^ *(\S+) *$/ ) { $item = $1; } else { my( $date, $person, $value ) = /^ {10}(.{10}) {5}(.*) (\S+)/; $person =~ s/ +$//; $date =~ s/^ +//; print $co_id, qq("$company"), $item, qq("$date"), qq("$person"), $value; } } __DATA__ Slate Enterprises, Inc.:2001050.01 104 1/24/2002 Johnson, Dean A. 10.00 1/24/2002 Botwell, Michael J 10.00 Total 104 20.00 302 1/25/2002 Beers, James T. 2.50 1/28/2002 Beers, James T. 4.00 1/29/2002 Beers, James T. 1.00 1/30/2002 Beers, James T. 0.50 Total 302 8.00 Total Slate Enterprises, Inc.:2001050.01 28.00 #### use Text::CSV_XS; my $csv = new Text::CSV_XS; . . . . . . $csv->combine( $co_id, $company, $item, $date, $person, $value ); print $csv->string, "\n";