Perl script (requires Text::CSV_XS):
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my @opt_columns;
GetOptions("column=i@" => \@opt_columns)
or die "Failed parsing options\n";
die "Must give at least one --column\n" if int(@opt_columns) == 0;
@opt_columns = map { $_-1 } @opt_columns; # convert 1-based to 0-based
use Text::CSV_XS;
my $csv = Text::CSV_XS->new ( { binary => 1 } );
open(my $stdin, "<-") or die "Couldn't open stdin\n";
open(my $stdout, ">-") or die "Couldn't open stdout\n";
while (my $row = $csv->getline($stdin)) {
my @nrow = @{$row}[@opt_columns];
$csv->print($stdout, \@nrow);
print "\n";
}
Put it in a file csvcut.pl
.
Example of taking only columns 3 and 4:
cat foo.csv | ./csvcut.pl --c 3 --c 4
This will only quote columns that need quoting, so if an input column has "Bar" (with quotes) it will come out Bar (without quotes).