You may be able to glean some understanding by reading the documentation for open. Also perlopentut may be helpful.
Take a stab at it, and when you get hung up on some specific part of your implementation of a solution, follow-up to this thread with some specific questions.
The general nature of this website tends to offer help and assistance in learning Perl. Requests for prewritten scripts generally aren't well received. If you're looking for someone to write a complete solution for you, post a job on http://jobs.perl.org.
Dave
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");
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 $arg0 $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");
You don't need perl for this. In the shell:
copy path\to\file1 + path\to\file2 + path\to\file3 path\to\newfileStrange things can happen if you want to copy binary files this way and forget the /B before the first argument (copy will stop at the first \cd). So:
copy /b path\to\file1 + path\to\file2 + path\to\file3 path\to\newfile
Strange things can happen if you want to copy binary files this way and forget the /B before the first argument (copy will stop at the first \cd)The solutions given so far will not deal with that, either. In a binary file, you're not at all guaranteed to have a newline. This (untested) code should work in the general case:
use warnings;
use strict;
open(my $out, ">", shift(@ARGV) ) or die $!;
my $buffer_length = 16_384;
while( my $file = shift(@ARGV) ) {
open(my $fh, $file) or die $!;
while( read($fh, my $buffer, $buffer_length) ) {
print $out $buffer or die $!;
}
close $fh;
}
close $out or die $!;
Season to taste.
thor
thor
perlmonks.org content © perlmonks.org and ambrus, Anonymous Monk, davido, Not_a_Number, saskaqueer, thor
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03