Re: How can I concatenate two files?
by Enlil (Parson) on Sep 02, 2004 at 20:01 UTC
|
use strict;
use warnings;
#OPEN FILE A.txt FOR APPENDING (CHECK FOR FAILURES)
open ( FOO, ">>", 'A.txt' )
or die "Could not open file A.txt: $!";
#OPEN FILE B.txt for READING (CHECK FOR FAILURES)
open ( BAR, "<", 'B.txt' )
or die "Could not open file B.txt: $!";
#READ EACH LINE OF FILE B.txt (BAR) and add it to FILE A.txt (FOO)
while ( my $line = <BAR> ) {
print FOO $line;
}
HTH-enlil | [reply] [d/l] |
|
print FOO <BAR>;
Or is this bad for some reason.
| [reply] [d/l] |
|
| [reply] |
|
That's not portable. You should use binmode on your handles.
Also, <BAR> could return an arbitrary long line. You can limit the size of that line using $/ = \4096;.
| [reply] [d/l] [select] |
Re: How to concatenate the contents of two files?
by fletcher_the_dog (Friar) on Sep 02, 2004 at 20:27 UTC
|
This little script will take any number of files given on the command line and concatenate them into the first file given on the command line:
my $outfile = shift;
open OUT,">".$outfile or die "Could not open $outfile:$!\n";
print OUT $_ while <>;
Command Line Version:
perl -e 'open OUT,">".shift;print OUT <>' output *.txt
| [reply] [d/l] [select] |
|
perl -please *.txt > output
Well, I suppose one could dub this a useless of use of Perl and simply do
cat *.txt > output
Makeshifts last the longest.
| [reply] [d/l] [select] |
|
copy /v B.txt A.txt
Or for multiple files
copy /v B.txt +C.txt A.txt
| [reply] [d/l] |
Re: How to concatenate the contents of two files?
by ccn (Vicar) on Sep 02, 2004 at 20:14 UTC
|
open A, '>> A.txt'
or die $!;
open B, '< B.txt'
or die $!;
print A <B>;
perldoc perlopentut | [reply] [d/l] |
|
With an eye on avoiding globals, with Two-arg open() considered dangerous in mind, and avoiding to slurping the entire input file or breaking it into lines when you're not going to need it split up that way anyway:
my $infile = "A.txt";
my $outfile = "B.txt";
open my $in_fh, '<', $infile
or die "Couldn't open $infile for reading: $!\n";
open my $out_fh, '>>', $outfile
or die "Couldn't open $outfile for appending: $!\n";
{
local $/ = \65536; # read 64kb chunks
while ( my $chunk = <$in_fh> ) { print $out_fh $chunk; }
}
Makeshifts last the longest.
| [reply] [d/l] |
Re: How to concatenate the contents of two files?
by davido (Cardinal) on Sep 03, 2004 at 02:38 UTC
|
Here is a strictly-Perl one-liner solution that works quite nicely.
First, the unix/linux version:
perl -ne 'BEGIN{open FH,">>", shift @ARGV or die $!} print FH $_;' A.t
+xt B.txt
And now the Windows version:
perl -ne "BEGIN{open FH,'>>', shift @ARGV or die $!} print FH $_;" A.t
+xt B.txt
The only difference is which quotes are used where, since Win32/DOS doesn't like single quotes much.
This works as follows:
- perl -n: Tell Perl to wrap the code inside a while( <> ){ ........ } block.
- Preceed that while(){} block with whatever code happens in the BEGIN{....} block. In this case, BEGIN shifts the first item off @ARGV, and opens it as a file in append mode.
- Now the while loop begins. The remaining arg on the command line (in @ARGV) becomes the file that's being iterated over in the while loop. As each line of B.txt is read, it's appended to the file represented by the FH filehandle (which happens to be A.txt).
Hope this helps!
| [reply] [d/l] [select] |
Re: How to concatenate the contents of two files?
by ihb (Deacon) on Sep 03, 2004 at 00:56 UTC
|
You've given several solutions, both Perl and non-Perl, but I thought I'd provide yet another short-cut. It uses the nice File::Slurp module. If you care about memory, don't use this method, but use the way described in Re: How can I concatenate two files? instead.
use File::Slurp qw/ read_file append_file /;
append_file('A.txt', read_file('B.txt'));
ihb
Read argumentation in its context! | [reply] [d/l] [select] |
Re: How to concatenate the contents of two files?
by crashtest (Curate) on Sep 02, 2004 at 20:37 UTC
|
If this is all you want to do with those files, you could just hand this off as a system command, like so:
`more B.txt >> A.txt`
See perlop if you're not familiar with "backtick" notation. | [reply] [d/l] |
|
system 'cat B.txt >> A.txt';
Makeshifts last the longest.
| [reply] [d/l] |