If you can assume the same number of fields in each line, you can you can try counting each possible delimiter for the first 5 lines or so, and seeing which returns a reasonable result.

This is a little rough, and could use better error checking, but something along these lines.

Run this in the directory containg the csv files. It assumes file extentions of .csv and saves the "corrected" files as filename.csv.new. Modify to suit.

Update: edited script slightly to remove useless use of array.

############################################### 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{$_}{tota +l}) > 2 and ($delim_count{$_}{total}/5 == int($delim_count{$_}{total} +/5))){ no warnings 'uninitialized'; $likely{$file} = $_ if ($delim_count{$_}{total} > $delim_c +ount{$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"; } }

In reply to Re^3: delimited files by thundergnat
in thread delimited files by saldoman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.