new tool
[hband-tools.git] / tabdata / csv2td
blob012219c37eca36e3a9fa7ceb9e3101683f872bd8
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 csv2td - Transform CSV to tabular data format.
9 =head1 DESCRIPTION
11 Read CSV data on STDIN.
12 Output tabular data to STDOUT.
14 =head1 OPTIONS
16 Any option which Text::CSV(3pm) takes.
17 See C<Text::CSV->known_attributes> for extensive list.
18 Example:
20 csv2td --sep=';' --blank-is-undef=0 --binary
22 becomes:
24 Text::CSV->new({sep=>";", blank_is_undef=>0, binary=>1})
26 =head1 F. A. Q.
28 Why there is no td2csv?
30 Why would you go back to ugly CSV when you have nice shiny Tabdata?
32 =head1 SEE ALSO
34 csv2(1), mrkv2td(1)
36 =cut
39 use Text::CSV qw/csv/;
40 use Data::Dumper;
41 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
42 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
45 %csv_opts = ();
47 for (@ARGV)
49 if(my ($optname, $value) = /^--([^=]+)(?:=(.*)|)$/)
51 $value = 1 if not defined $value;
52 $optname =~ s/-/_/g;
53 $csv_opts{$optname} = $value;
55 else
57 die "$0: unknown argument: $_\n";
61 $Data = csv(in=>\*STDIN, %csv_opts);
63 for my $row (@$Data)
65 print join($FS, map {escape_tabdata($_)} @$row).$RS;