Skip to content

Instantly share code, notes, and snippets.

@jreisinger
Created April 16, 2013 10:55
Show Gist options
  • Save jreisinger/5395058 to your computer and use it in GitHub Desktop.
Save jreisinger/5395058 to your computer and use it in GitHub Desktop.
Filter out records by column and regular expression
#!/usr/bin/perl
# Filter out records by column and regular expression
use strict;
use warnings;
use 5.010;
use Text::CSV_XS;
my $file = shift;
my $separator = shift // ';';
my $csv = Text::CSV_XS->new( { binary => 1, sep_char => $separator } )
or die "Cannot use CSV: " . Text::CSV_XS->error_diag();
binmode STDOUT, ":utf8";
open my $fh, "<:encoding(utf8)", $file or die "$file: $!";
while ( my $row = $csv->getline($fh) ) {
$row->[11] =~ m/\w\w_\w+/ or next; # column number and regex
say join ";", @$row;
}
$csv->eof or $csv->error_diag();
close $fh;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment