print "\n" x 3, "Running on $^O", "\n" x 3; # User must change the path below to own directory # containing only *.csv files by Pimento Software as # output by their Roadrunner hardware. my $dir = 'K:/VaLab/gus_csv_pl/'; # Below is for author's use on own NetBSD (Unix) box. $dir = '/spare/gus_perl/gus_csv_pl/' if $^O eq 'netbsd'; # Re-write Pimento-generated CSV as standard IETF CSV. sub fix_pimento_csv { my ($in_file, @cols) = @_; my $line; my @keys; my @lines; if (open CSV, "<$in_file") { print "\nReading suspect CVS from: $in_file \n"; $line = ; print "1st line says:\n$line"; $line = ''; while () { $_ =~ s/\s/ /g; $line .= $_ if ($_ =~ /^,C/) || ($_ =~ /,,/); last if $_ =~ /,,/; } $line =~ s/^,//; # Lose leading null field. $line =~ s/,,.*//; # Lose null field and empty fields. $line =~ s/\n//; # Lose extraneous line delimiter. #$line =~ s/C[0-9]+\s\(M\):\s*//g; # Lose chan enumerators. @keys = split ',', $line; # Return array of keys for parser. for (0 .. $#keys) { $keys[$_] = "'$keys[$_]'"} # Quote column keys. $line = join ',', @keys; # Header is CSV standardized now. push @lines, $line; while () { chomp $_; $_ =~ s/\s/ /g; $_ =~ s/^,//; # Lose leading null field. $_ =~ s/,$//; # Lose trailing null field. push @lines, $_; } close CSV; my $out_file = $in_file; $out_file =~ s/csv$/ietf.csv/; if (open IETF_CSV, ">$out_file") { print "Writing IETF CSV to: $out_file \n"; foreach (@lines) { print IETF_CSV "$_\n"; } close IETF_CSV; } else { print "Oops! Problem writing to '$out_file': $! \n" } } else { print "Oops! Problem reading from '$in_file': $! \n" } } sub fix_all_pimentos { my $dir = shift; while ( glob "$dir*.csv" ) { next if $_ =~ /ietf\.csv/; fix_pimento_csv($_); } print "\nAll done. \n\n"; } fix_all_pimentos($dir);