http://qs1969.pair.com?node_id=388082

yankeeblue has asked for the wisdom of the Perl Monks concerning the following question:

I'm a newbie to Perl so any help will be greatly appreciated. I'm having a problem figuring out how to concatenate the contents of two files.

I basically have file A.txt and file B.txt. I would like the contents of file B.txt appended to the end of file A.txt.

I've tried a bunch of things and I can't get it to work. I tried opening a file handle for A.txt (for appending), and print the file B.txt to it, but all I got was the line B.txt at the end of the file A.txt (obviously wrong).

TIA for your help.
  • Comment on How to concatenate the contents of two files?

Replies are listed 'Best First'.
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

      What about?
      print FOO <BAR>;
      Or is this bad for some reason.

        Doesn't that load up the entire file into memory? It could be a problem with bigr files.

      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;.

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

      Commandline version:

      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.

        Or the dos version
        copy /v B.txt A.txt Or for multiple files copy /v B.txt +C.txt A.txt
Re: How to concatenate the contents of two files?
by ccn (Vicar) on Sep 02, 2004 at 20:14 UTC

      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.

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:

    1. perl -n: Tell Perl to wrap the code inside a while( <> ){ ........ } block.
    2. 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.
    3. 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!


    Dave

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!

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.

      Ugh, please don't use backticks simply to invoke a shell command.

      system 'cat B.txt >> A.txt';

      Makeshifts last the longest.