Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Text merge concept in perl

by Redsun (Initiate)
on Dec 19, 2013 at 06:57 UTC ( #1067788=perlquestion: print w/ replies, xml ) Need Help??
Redsun has asked for the wisdom of the Perl Monks concerning the following question:

How to merge multiple txt files from single directory merge as a single txt file. Please tell any one?

Comment on Text merge concept in perl
Re: Text merge concept in perl
by mtmcc (Hermit) on Dec 19, 2013 at 08:21 UTC
    Everything you need to know is in the Tutorials section.

    If you get stuck with a particular point of your code, do ask again.

    EDIT: of particular use might by readdir, open, and perlsyn.

Re: Text merge concept in perl
by hippo (Deacon) on Dec 19, 2013 at 09:46 UTC

    You'll have to define "merge". Without any particular algorithm this could be as simple as

    cat *.txt > outfile
Re: Text merge concept in perl
by karlgoethebier (Curate) on Dec 19, 2013 at 10:17 UTC
Re: Text merge concept in perl
by Lennotoecom (Monk) on Dec 19, 2013 at 11:53 UTC
    open OUT,'>merged.txt' or die $!; print OUT $_ foreach (<>);
    don't forget to run it like:
    ./a.pl /home/user/*.txt
Re: Text merge concept in perl (Path::Tiny
by Anonymous Monk on Dec 19, 2013 at 17:35 UTC

    See Path::Tiny, File::Find::Rule

    #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; use File::Find::Rule qw/ find /; Main( @ARGV ); exit( 0 ); sub Main { #~ MainReaddir( @_ ); MainRule( @_ ); } sub MainRule { my( $dir , $mergeinto ) = @_; my $mergefh = path( $mergeinto )->opena_utf8; my $readdir = path( $dir )->realpath; my @entries = find( file => maxdepth => 1, name => qr/\.txt$/, in => $readdir, ); for my $name ( @entries ){ my $entry = path( $name ); if( $entry->is_file and $name =~ /\.txt$/ ){ my $inputfh = $entry->openr_utf8; Mergerations( $mergefh, $inputfh ); close $inputfh; } } close $mergefh; } sub MainReaddir { my( $dir , $mergeinto ) = @_; my $mergefh = path( $mergeinto )->opena_utf8; my $readdir = path( $dir )->realpath ->iterator( { qw/ recurse 0 / }); while( my $entry = $readdir->() ){ if( $entry->is_file and $entry =~ /\.txt$/ ){ my $inputfh = path( $entry )->openr_utf8; Mergerations( $mergefh, $inputfh ); close $inputfh; } } } sub Staterator { my( $out, $in ) = @_; ... } __END__

    see perlintro, also chromatics free book Modern Perl a loose description of how experienced and effective Perl 5 programmers work....You can learn this too.
    Learn Perl in about 2 hours 30 minutes

Re: Text merge concept in perl
by Laurent_R (Vicar) on Dec 19, 2013 at 19:02 UTC
    Or this one-liner (I am merging here all the a*.pl files in my current directory into out.txt):
    $ perl -pe '' a*.pl > out.txt
    Just a quick check on the number of lines and characters:
    $ wc out.txt 186 583 4219 out.txt $ wc a*.pl 11 27 187 a.pl 35 125 719 add2.pl 11 47 305 analyse_fable.pl 25 95 502 any.pl 24 99 532 any2.pl 24 55 357 aoh.pl 56 139 1617 array_hash.pl 186 587 4219 total

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-07-23 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (133 votes), past polls