Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: compare and merge files

by Adrade (Pilgrim)
on Feb 06, 2006 at 02:54 UTC ( [id://528139]=note: print w/replies, xml ) Need Help??


in reply to compare and merge files

perl -e 'for(@ARGV){open(I,"<$_");s/\.[^\.]*\-[^\-]*$/.MERGED.txt/;ope +n(O,">>$_");print O $_ while <I>;close(O);close(I)}' file.abc.part-1. +txt file.abc.part-2.txt file.abc.part-3.txt file.def.part-1.txt file. +def.part-2.txt

--
By a scallop's forelocks!

Replies are listed 'Best First'.
Re^2: compare and merge files
by graff (Chancellor) on Feb 06, 2006 at 06:12 UTC
    I'm all for one-liners and golfing, etc, but... Did you know that you can actually include line-feeds when typing in a command-line script like this? You post would work the same if entered as:
    perl -e 'for(@ARGV){ open(I,"<$_"); s/\.[^\.]*\-[^\-]*$/.MERGED.txt/; open(O,">>$_");print O $_ while <I>;close(O); close(I) }' file.abc.part-1.txt file.abc.part-2.txt file.abc.part-3.txt file.de +f.part-1.txt file.def.part-2.txt
    The line still gets a little long when all those file names need to be filled in too. (Why didn't you use "*.part-*" for that?)

    My point is, I've been seeing a lot of this one-liner stuff being offered recently, to people who probably can only scratch their heads and say "That should have been posted in the Obfu wing...".

    You'd get more appreciation (and more people would actually be more likely to learn your nifty tricks) if you made it at least a little more legible.

      I guess I sort of just saw the question and wanted to see if I could quickly pound out an answer. I really should have explained things more, probably, though I didn't really think my result was that obfu. I definitely didn't mean to confuse anyone.

      Also, I didn't use *.part-* since it didn't seem like the OP was looking to merge all the files in the dir, since the existence of file.mno.part-5.txt seems to imply the existence of file.mno.part-1.txt, file.mno.part-2.txt, etc., but only the former was listed. Incidentally, I just noticed that the OP wanted to use a file as input, in which case she or he could just replace for(@ARGV){ with while(<>){chomp; and use the file with the file locations as the only argument. For those that don't know, the diamond thingie is the magical input dingdong that pulls in input from the files listed in the arguments. When used in a while context like this, its the same as saying while (defined($_ = <>)) { - which is sort of magical too.

      Anyway, Graff, thanks for pulling me back down to earth a little to sort of remember that we're all here to share and learn. Of course, apologies to any who may have been confused.

      Best,
        -Adam

      P.S. To anyone interested, you can also remove the close()s, since open() close()s filehandles of the same name should they be open, and any filehandles remaining open are closed at the scripts end.

      --
      By a scallop's forelocks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-24 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found