Created
November 5, 2019 07:07
-
-
Save epixoip/96fc3baa3992b1b13c7bcf92f4c13bd4 to your computer and use it in GitHub Desktop.
Brute force field delimiters in a text file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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