#!/usr/bin/perl/ use strict; use warnings; use Data::Dumper; use Time::Piece; my $filename = 'uncleaned.csv'; # Open input file open my $FH, $filename or die "Could not read from $filename <$!>, program halting."; # Open error handling file open ( my $ERR_FH, '>', "errorFiles.csv" ) or die $!; # Read the header line of the input file and print to screen. chomp(my $line = <$FH>); my @fields = split(/,/, $line); print Dumper(@fields), $/; my @data; # Read the lines one by one. while($line = <$FH>) { chomp($line); # Scrub data of characters that cause scripting problems down the line. $line =~ s/[\'\\]/ /g; # split the fields of each record my @fields = split(/,/, $line); # Check if the storeNbr field is empty. If so, write record to error file. if (!length $fields[28]) { print $ERR_FH join (',', @$_), $/ for @data; } else { # Concatenate the first three fields and add to the beginning of each record unshift @fields, join '_', @fields[28..30]; # Format the DATE fields for MySQL $_ = join '-', (split /\//)[2,0,1] for @fields[10,14,24,26]; # Scrub colons from the data $line =~ s/:/ /g; # If Spectro_Model is "UNKNOWN", change if($fields[22] eq "UNKNOWN"){ $_ = 'UNKNOW' for $fields[22]; } # If tran_date is blank, insert 0000-00-00 if(!length $fields[10]){ $_ = '0000-00-00' for $fields[10]; } # If init_tran_date is blank, insert 0000-00-00 if(!length $fields[14]){ $_ = '0000-00-00' for $fields[14]; } # If update_tran_date is blank, insert 0000-00-00 if(!length $fields[24]){ $_ = '0000-00-00' for $fields[24]; } # If cancel_date is blank, insert 0000-00-00 if(!length $fields[26]){ $_ = '0000-00-00' for $fields[26]; } # Format the PROD_NBR field by deleting any leading zeros before decimals. $fields[12] =~ s/^\s*0\././; # put the records back push @data, \@fields; } } close $FH; close $ERR_FH; print "Unsorted:\n", Dumper(@data); #, $/; #Sort the clean files on Primary Key, initTranDate, updateTranDate, and updateTranTime @data = sort { $a->[0] cmp $b->[0] || $a->[14] cmp $b->[14] || $a->[26] cmp $b->[26] || $a->[27] cmp $b-> [27] } @data; open my $OFH, '>', '/path/cleaned.csv'; print $OFH join(',', @$_), $/ for @data; close $OFH; exit;