Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How to concatenate the contents of two files?

by davido (Cardinal)
on Sep 03, 2004 at 02:38 UTC ( [id://388193]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://388193]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found