############################################### use warnings; use strict; my @delimiters = ('_', '*', '&', '^', '%', '$', '#', '@', '!', '~', '`', '<', '>', '.', ':', ';', '€', 'œ', 'þ', ','); my @files = glob('*.csv'); # or whatever my %likely; for my $file(@files){ open my $fh, '<', $file or warn "Couldn't open $file. $!"; my %delim_count; for my $count (1..5){ my $line = <$fh>; for (@delimiters){ my $testline = $line; $delim_count{$_}{total} += $testline =~ s/\Q$_\E//g; } } for (@delimiters){ if (defined $delim_count{$_}{total} and ($delim_count{$_}{total}) > 2 and ($delim_count{$_}{total}/5 == int($delim_count{$_}{total}/5))){ no warnings 'uninitialized'; $likely{$file} = $_ if ($delim_count{$_}{total} > $delim_count{$likely{$file}}{total}); } } print "Most likely delimiter for $file is $likely{$file}\n" } for my $file (keys %likely){ if (defined $likely{$file}){ print "Updating $file....\n"; next if ($likely{$file} eq ','); my ($csv,$output); unless (open $csv, '<', $file){ warn "Couldn't open $file. $!"; next; } unless (open $output, '>', "$file.new"){ warn "Couldn't open $file.new for writing. $!"; next; } while (<$csv>){ s/\Q$likely{$file}\E/,/g; print $output $_; } }else{ print "Ambiguous delimiter for file $file\n"; } }