"mig" tool
[migration-tools.git] / csv_ripper
1 #!/usr/bin/perl
2
3 # Copyright 2009-2012, Equinox Software, Inc.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 # TODO
20 #
21 # * add getopt support
22 # * use tag generator to expand list of wanted fields?
23 # * insertion of constant-value fields
24 # * field mapping ("some text" -> 4)?
25
26 # csv-extractor FILENAME FIELD_LIST
27
28 use strict;
29 use warnings;
30
31 use Text::CSV;
32
33 my $csv = Text::CSV->new( {binary => 1});
34
35 open CSV, '<', shift or die "Can't open file $!\n";
36
37 while (<CSV>) {
38     $csv->parse($_);
39     die "Bad data on line $.\n$_\n" if ($csv->error_diag);
40     my @chunks = $csv->fields;
41     my @out = ();
42     for my $chunk (@ARGV)
43       { push @out, $chunks[$chunk] }
44     print join("\t", @out), "\n";
45 }