Created
April 16, 2013 10:55
-
-
Save jreisinger/5395058 to your computer and use it in GitHub Desktop.
Filter out records by column and regular expression
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/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