#!/usr/bin/perl

use strict;

use IO::Handle;
use Text::CSV_XS;
my $CSV = new Text::CSV_XS { binary => 1 };

my $n = 0;
my $r = "";

sub csv2tab {
	my $f = shift;
	open my $fh, "< $f"
		or die "$0: $f: $!\n";
	while (not eof($fh)) {
		my $ary = $CSV->getline($fh)
			or die "$0: bad CSV input at $f line $.\n";
		next unless @$ary;
		s/[[:cntrl:]]/ /g for @$ary;
		my $n = $n || @$ary;
		@$ary = map { $_ ne "" ? $_ : $r } @$ary[0..$n-1];
		print join("\t", @$ary), "\n";
	}
}

use autouse qw(Pod::Usage pod2usage);
use Getopt::Long 2.24 qw(GetOptions :config gnu_getopt);
GetOptions
	"n|fields=i"	=> \$n,
	"r|relpace=s"	=> \$r,
	"h|help"	=> sub { pod2usage("00") }
		or pod2usage(2);

csv2tab($_) foreach @ARGV ? @ARGV : "-";

__END__

=head1	NAME

csv2tab - convert comma-separated values into tab-delimited fields

=head1	SYNOPSIS

B<csv2tab>
[B<-h>|B<--help>]
[B<-n>|B<--fields>=I<NF>]
[B<-r>|B<--relpace>=I<STR>]
[I<FILE>...]

=head1	DESCRIPTION

It does work on files with newlines in records.  Control characters
in each field (including tabs and newlines) are replaced with spaces.

=head1	OPTIONS

=over

=item	B<-n>, B<--fields>=I<NF>

Extract I<NF> fields from each line.

=item	B<-r>, B<--replace>=I<STR>

Replace empty fields with I<STR>.

=item	B<-h>, B<--help>

Display this help and exit.

=back

=head1	AUTHOR

Written by Alexey Tourbin <at@altlinux.org>.

=head1	COPYING

Copyright (c) 2005 Alexey Tourbin, ALT Linux Team.

This is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.

=head1	SEE ALSO

Text::CSV_XS, cut(1), awk(1)

=cut
