use Text::CSV_XS; use Date::Manip; sub trim($); # Formatting variables $quote_char = '"'; $sep_char = ','; $NumHeaderRowsToSkip = 6; #$AuctionSite_EmailBlank = "amazon|pay"; $out=$ARGV[0]; # Name output file $out =~ s/(^.*).csv/$1-FS.txt/; # open file handle for output file open OUT, ">$out" or die "Cannot open $out for write :$!"; # Remove linefeeds and build array while (<>) { chomp; push (@rows, $_); } # Init csv object $csvin = Text::CSV_XS->new({ 'quote_char' => $quote_char, 'sep_char' => $sep_char }); $csvout = Text::CSV_XS->new({ 'quote_char' => $quote_char, 'sep_char' => ",", 'always_quote' => true }); # build array of header fields $headers = $rows[0]; $csvin->parse($headers); my @headers = $csvin->fields; my @rowscolumns = map { chomp; $csvin->parse($_); [$csvin->fields]; } @rows; # build array of hashes with column name as hash index foreach my $row(@rowscolumns) { my %hashrow; my $index = 0; foreach my $val(@$row) { $hashrow{trim($headers[$index])} = trim($val); $index++ } push @arrayhash, \%hashrow; } $arrayhash[0]->{'Reference'} = 'Reference'; $arrayhash[0]->{'trackingno'} = 'trackingno'; $arrayhash[0]->{'packwt'} = 'packwt'; $arrayhash[0]->{'shipdate'} = 'shipdate'; $arrayhash[0]->{'paytype'} = 'paytype'; # construct output csv lines my $index = 0; foreach my $row(@arrayhash) { if($index>0) { $row->{'shipdate'} = &UnixDate(&ParseDate($row->{'shipdate'}),"%m/%d/%Y"); } my @output_fields = ( $row->{'Reference'}, $row->{'trackingno'}, $row->{'packwt'}, $row->{'shipdate'}, $row->{'paytype'}, ); $csvout->combine(@output_fields); $outrow[$index] = $csvout->string; $index++; } #print result to file foreach (@outrow) { print OUT "$_\n"; } sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; }