in reply to How do I append more than one files?

This script does something like what you want:

#!perl -w =head1 USAGE ./combine.pl output_file input_1 input_2 [..., input_x] Where output_file is the file to which all input files (input_1, input_2, ..., input_x) should be combined to. =cut use strict; my $out = shift @ARGV; open( my $out_fh, '>', $out ) or die("Could not open output file '$out': $!\n"); for my $file (@ARGV) { open( my $in_fh, '<', $file ) or die("Error opening '$file': $!\n"); print $out_fh $_ while (<$in_fh>); close($in_fh) or die("Error closing '$file': $!\n"); print $out_fh "\n"; } close($out_fh) or die("Error closing '$out': $!\n");

Replies are listed 'Best First'.
Re^2: How do I append more than one files?
by Anonymous Monk on Jun 16, 2004 at 23:50 UTC

    This is certainly useful but in essence, would this take a longer time than usual? The reason being - we have 12 log files, running on 6 clones, and we are required to keep them for xx days but would like to 'cat' them ala Unix but the app server is running on Wintel boxes.

    So far the closest thing I can find in the ActivePerl manual is this command line:

    perl -MExtUtils::Command -e cat file1 file2 .. > merge_file

    If I want to pass the files as parameters, can I call the above command within another perl script? So I would have a Perl script called catFiles.pl file1 file 2 > merge_file and in the script, I would have:

    perl -MExtUtils::Command -e cat $arg[0] $arg1 > $arg3

    but it doesnt seem to work. At the moment, I could get it to work by creating a dos batch and passing the args to the perl command, but it is not ideal. Thanks again

      I think you'd want something like this to create your catFiles.pl script:

      #!perl # called as catFiles.pl input1 input2 > output exec( qw(perl -MExtUtils::Command -e cat), @ARGV );

      Or if you want a different way of executing the command:

      #!perl -w # called as catFiles.pl output input1 input2 use strict; my ($output, @inputs) = @ARGV; exec("perl -MExtUtils::Command -e cat @inputs > $output");