#!/usr/bin/env perl # # csvcombine - combine multiple CSV files into one # # $Id$ use warnings; use strict; use Getopt::Std; use Text::CSV_XS; my %opt; getopts( 'hft:', \%opt ); &help if $opt{'h'}; my $sep = $opt{'t'} // ','; my $add_header = $opt{'f'} // 0; @ARGV = ('-') if !@ARGV; sub help { print STDERR <; } elsif ( !open( my $infh, '<', $f ) ) { warn "cannot open for reading, skipped: $f\n"; next; } else { @lines = <$infh>; close($infh); } chomp for @lines; $fn2lines{$f} = [@lines]; my $nrlines = scalar(@lines); $maxnrlines = $nrlines if $nrlines > $maxnrlines; } if ($add_header) { print join( $sep, @ARGV ), $/; } foreach my $i ( 0 .. $maxnrlines - 1 ) { my @combined = map { $ARGV[$_] eq '+' ? $i : ( $ARGV[$_] =~ /^=(-?\d+)$/ ? $1 : $fn2lines{ $ARGV[$_] }->[$i] // '' ) } 0 .. $#ARGV; print join( $sep, @combined ), $/; }