Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Perl script for concatenating multiple files in aspecific order listed in a txt file

by Anonymous Monk
on Aug 11, 2017 at 14:33 UTC ( [id://1197256]=perlquestion: print w/replies, xml ) Need Help??

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

Assalm o alikum everyone!

I want to concatenate 100 of FASTA file present in single directory acccording to a specfic order listed in a file by using perl kindly guide me how to do so >???

  • Comment on Perl script for concatenating multiple files in aspecific order listed in a txt file

Replies are listed 'Best First'.
Re: Perl script for concatenating multiple files in aspecific order listed in a txt file
by hippo (Bishop) on Aug 11, 2017 at 14:39 UTC
    $ xargs cat < list > outfile
    by using perl

    Well you could, but then that would be more work for you and slower besides. Is this an XY Problem?

      Not OP, but perl scripts can be written in a way so as to be cross platform
Re: Perl script for concatenating multiple files in aspecific order listed in a txt file
by Your Mother (Archbishop) on Aug 11, 2017 at 15:37 UTC

    It would be semi-trivial with Path::Tiny. You should read the stat/-X docs to know how to make it a bit more safe and robust.

    Show us some code you've tried if you're having trouble getting it to work and someone will be glad to help fix or adapt it.

Re: Perl script for concatenating multiple files in aspecific order listed in a txt file
by Laurent_R (Canon) on Aug 11, 2017 at 20:53 UTC
    To do it Perl, read the list file into an array, then loop on the array, open each file listed in turn and print what you read to the same output file.
      To do it Perl, read the list file into an array, then loop on the array, open each file listed in turn and print what you read to the same output file.

      ... and don't forget to chomp off the trailing newlines.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Well, chomping the lines from the input files may or may not be necessary. There is no point removing the new lines if the next print statement is just adding them back. So it really depends on what the input files look like.

        It may even not be necessary to read the input files line by line and possibly faster to read 4-kB blocks or so.

Re: Perl script for concatenating multiple files in aspecific order listed in a txt file
by BillKSmith (Monsignor) on Aug 11, 2017 at 17:32 UTC
    Are you certain that a simple concatenate is all you need? Consider what would happen if one or more of the files did not end with a newline. Are there any other special cases?
    Bill
Re: Perl script for concatenating multiple files in aspecific order listed in a txt file
by thanos1983 (Parson) on Aug 11, 2017 at 18:19 UTC

    Hello Anonymous Monk,

    I see already the fellow Monks have provided you with plenty of options. I just would like to provide two more possible solutions.

    But I still lost, what is the a specific order of the files in your file? Is it in numerical order?

    Since you did not provide us with an example of your file and expected output I will just assume, and propose something. Why not extract all FASTA files from the file in an array and simply concatenate them with the join?

    Just a suggestion if I have understand correctly.

    Provide us more information so we can help you.

    Solution one using File::Slurp/read_dir:

    Solution two using readdir:

    Sorry I miss understood the question.

    Looking forward to your update, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

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

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

    No recent polls found