Skip to content

Instantly share code, notes, and snippets.

@laouji
Created December 11, 2018 13:38
Show Gist options
  • Save laouji/377f5ea20880e70c91b6b52a7f357d9d to your computer and use it in GitHub Desktop.
Save laouji/377f5ea20880e70c91b6b52a7f357d9d to your computer and use it in GitHub Desktop.
csv column counter script
#!/bin/env perl
use warnings;
use strict;
use Text::CSV;
my $filename = $ARGV[0];
if (!$filename) {
print "filename must be passed as an argument\n";
exit(2);
}
my $i = 0;
my $expected_column_count = 0;
my $csv = Text::CSV->new({binary => 1}) or die "cannot use CSV: ".Text::CSV->error_diag();
my $fh = IO::File->new("< ${filename}");
if (defined $fh) {
while (defined(my $row = $csv->getline($fh))) {
$i++;
my $column_count = scalar(@$row);
if ($i == 1) {
$expected_column_count = $column_count;
}
if ($expected_column_count != $column_count) {
printf("expected %d columns but found %d on line %d\n", $expected_column_count, $column_count, $i);
next;
}
}
$fh->close;
}
print("all done\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment