-
Notifications
You must be signed in to change notification settings - Fork 1
/
csvcut
executable file
·34 lines (29 loc) · 883 Bytes
/
csvcut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long qw{ GetOptions };
use Text::CSV_XS;
GetOptions('d=s' => \ (my $delimiter = ','),
'f=s' => \ (my $fields = '1-')
);
$fields =~ /^
(?:[0-9]+
(?: - [0-9]* )?
,)*
[0-9]+
(?: - [0-9]* )?
$/x or die "Invalied -f: $fields";
$fields =~ s/-/../g;
$fields =~ s/-(?=,|$)/END/g;
my $csv = 'Text::CSV_XS'->new({binary => 1,
auto_diag => 1,
sep_char => $delimiter,
always_quote => 1});
for my $file (@ARGV) {
open my $in, '<:encoding(UTF-8)', $file or die "$file: $!";
my @fields;
while (my $row = $csv->getline($in)) {
@fields = eval ($fields =~ s/END/$#$row/gr) unless @fields;
$csv->say(*STDOUT, [@$row[@fields]]);
}
}