#!/usr/bin/perl # test.pl use strict; use warnings; use Text::CSV_XS; use File::BOM; use Switch; my @rows; my @strings; my $CT = 0; my $i = 0; my %i = {$i}; my $input = "000"; # read in argument and use the first one as the filename to be read if (! defined $ARGV[0]){ print "Usage: ./convertACsv.pl \"Please specify the filename like \"123456.csv\"\"\n"; exit; } my $FileName = $ARGV[0]; # In a hex editor our source file shows \n or 0x0A or \010 characters in the data fields. # Our next process wants to have all fields within quotes and separated by commas. my $aCSV = Text::CSV_XS->new ({ eol => undef, # \r, \n, or \r\n sep_char => ',', sep => undef, quote_char => '"', quote => '"', escape_char => '"', binary => 1, decode_utf8 => 1, auto_diag => 1, diag_verbose => 1, blank_is_undef => 0, empty_is_undef => 0, allow_whitespace => 1, allow_loose_quotes => 1, allow_loose_escapes => 0, allow_unquoted_escape => 0, always_quote => 1, quote_empty => 0, quote_space => 0, escape_null => 1, quote_binary => 1, keep_meta_info => 0, verbatim => 0, types => undef, callbacks => undef, }); open my $ORIG_CSV, '<:via(File::BOM)', "C:\\A\\$FileName" or die "Can't open C:\\A\\$FileName: $!"; open my $MOD_CSV, '>:raw:encoding(iso-8859-1)', "C:\\A\\ModCSV.csv" or die "Can't open C:\\A\\ModCSV.csv: $!"; open my $KBan_CSV, '>:raw:encoding(iso-8859-1)', "C:\\A\\kban.csv" or die "Can't open C:\\A\\kban.csv: $!"; open my $CT_CSV, '>:raw:encoding(iso-8859-1)', "C:\\A\\ct.csv" or die "Can't open C:\\A\\ct.csv: $!"; while (my $row = $aCSV->getline ($ORIG_CSV)) { if ($row->[0] =~ /\S/ ) { #many regex data modifiers removed from here if ($row->[1] =~ /^\d+/) { $i{$row->[3]}+=$row->[1]; print "$i{$_} $_+\n for sort keys %i"; } # post processing removed push @rows, $row; } close $ORIG_CSV; $aCSV->say ($MOD_CSV, $_) for @rows; close $MOD_CSV or die "Can't close C:\\A\\ModCSV.csv: $!"; close $KBan_CSV or die "Can't close C:\\A\\KBan.csv: $!"; close $CT_CSV or die "Can't close C:\\A\\CT.csv: $!"; }