Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

File Copy/Merge Question

by parkprimus (Sexton)
on Feb 13, 2004 at 19:46 UTC ( [id://328864]=perlquestion: print w/replies, xml ) Need Help??

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

Monks,
I have a general question about copying files. I have 5 rather large files that I need to merge into one. Does anyone know of any modules that will do this? I went to cpan but I did not quite find what I was looking for. Thanks for you help.

--parkprimus

Replies are listed 'Best First'.
Re: File Copy/Merge Question
by thelenm (Vicar) on Feb 13, 2004 at 20:16 UTC
    What do you mean by "merge"? Do you just need to concatenate the files? If so, you don't need Perl for this. Use this on *nix:
    cat file1 file2 file3 file4 file5 > bigfile

    -- Mike

    --
    XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
    -- grantm, perldoc XML::Simpler

Re: File Copy/Merge Question
by diotalevi (Canon) on Feb 13, 2004 at 20:05 UTC

    I don't know of a module but this is dirt easy to do with standard perl. Here's a quicky program for you. I tried to demonstrate good habits - 5.005 compliance, safe-two-arg open, localizing filehandles before use, platform independance with binmode, and checking system calls for errors.

    use strict; use vars qw( $DESTINATION @SOURCES ); $DESTINATION = pop @ARGV; @SOURCES = @ARGV; local *DEST; open DEST, "> $DESTINATION\0" or die "Can't overwrite $DESTINATION: $!"; binmode *DEST or die "Can't binmode $DESTINATION: $!"; for my $file_name ( @SOURCES ) { local *IN; open IN, "< $file_name\0" or die "Can't open $file_name for readin +g: $!"; binmode *IN or die "Can't binmode $file_name: $!"; local $/ = \ 8192; while ( my $chunk = <IN> ) { print DEST $chunk or die "Can't write to $DESTINATION: $!"; } close IN or warn "Can't close $file_name after reading: $!"; } close DEST or warn "Can't close $DESTINATION after writing: $!";
Re: File Copy/Merge Question
by BazB (Priest) on Feb 13, 2004 at 21:07 UTC

    File::MergeSort or File::Sort will allow you to merge files.

    Disclaimer: I maintain File::MergeSort, patches welcome :-)


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Re: File Copy/Merge Question
by BrowserUk (Patriarch) on Feb 13, 2004 at 21:46 UTC

    This one liner will do it. If you have 5.8.1 or later, it is also safe to use on binary files on win32.

    perl -C3 -pe1 file1 file2 file3 > all

    And it's a lot easier to type than copy file1/b + file2/b + file3/b all/b which is the native command for this.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!
Re: File Copy/Merge Question
by Art_XIV (Hermit) on Feb 13, 2004 at 21:13 UTC

    Here's the extra-quick but naughty version. It should work just peachy with concatenating text files.

    use strict; use warnings; my $destination = pop @ARGV; open OUT, ">$destination" or die "Couldn't create the concatenated file $destination: $!\n"; print OUT while (<>); close OUT;

    The '<>' is a spicy bit of Perl that uses your command-line args as file names.

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
      That's actually rather unsafe. See Dangerous diamonds!. This is why I was using the "< $file\0" form. If I were coding to 5.8 then I'd use three-arg open. You also just destroyed any binary data if this is to be done on a Windows computer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2025-06-17 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.