Brute force field delimiters in a text file
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
my @delims = ( 9, 11, 28, 29, 30, 31, 32 .. 47, 58 .. 64, 91 .. 96, 123 .. 126 ); | |
my $file = $ARGV[0] || die "Usage: $0 <filename>\n"; | |
open (my $fh, "<", $file) || die "Unable to open $file: $!\n"; | |
my $line = 0; | |
while (my $row = <$fh>) { | |
my %occur = (); | |
chomp $row; | |
foreach my $delim (@delims) { | |
my $char = chr $delim; | |
$occur{$delim} = length( $row =~ s/[^\Q$char\E]//rg ); | |
} | |
my @occurs = sort { $occur{$a} <=> $occur{$b} } keys %occur; | |
printf ("Line %s appears to be '%s' (chr %d) delimited\n", $line++, chr $occurs[-1], $occurs[-1]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment