Skip to content

Instantly share code, notes, and snippets.

@epixoip
Created November 5, 2019 07:07
Show Gist options
  • Save epixoip/96fc3baa3992b1b13c7bcf92f4c13bd4 to your computer and use it in GitHub Desktop.
Save epixoip/96fc3baa3992b1b13c7bcf92f4c13bd4 to your computer and use it in GitHub Desktop.
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