in reply to How to concatenate the contents of two files?

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