in reply to Merge and split files based on number of lines
I have created a quick experiment based on your question. Not sure what it is worth but I tried to use the 'chunk_size' facility in MCE to achieve what you want
Since you didn't show any data to test on I had to make some assumptions on how it looks like and created a script to generate some data files
The program starts up a Perl data reader program using open3 and feeds the results to the MCE 'mce_loop_f'. The chunk size eventually determines how many lines are being output in a new file. Files are written to a 'output' subfolder
I say again, just is just a funny experiment of mine and maybe someone else can say something about performance, but hey, it might do the job!
test.pl
use strict ; use warnings ; use IPC::Open3 qw( open3 ) ; use MCE::Loop ; MCE::Loop::init { max_workers => 3, chunk_size => 10 } ; my $cmd = 'perl readfiles.pl' ; sub do_work { my @ar = @{$_[0]} ; my $id = $_[1] ; open( my $fho, ">", "output\\$id.txt" ) or die "Can't open > outpu +t\\$id.txt: $!" ; foreach ( @ar ) { print $fho $_ ; } close($fho) ; } my $pid = open3( undef, my $filedata, undef, $cmd, @_, ) ; print STDERR "Starting MCE loop\n" ; mce_loop_f { my ( undef, $chunk_ref, $chunk_id ) = @_ ; do_work( $chunk_ref, $chunk_id ) ; } $filedata ; print STDERR "Ended\n" ; waitpid( $pid, 0 ) ; warn "Failed\n" if ( $? << 8 != 0 ) ; close($filedata) ;
readfiles.pl
use strict ; use warnings ; my @files = qw( data1.txt data2.txt data3.txt ) ; foreach ( @files ) { open( my $fh, "<", $_ ) or die "Can't open < $_: $!" ; while(<$fh>) { chomp; print $_ . "\n" ; } close( $fh ) ; }
createdatafiles.pl
use strict ; use warnings ; open( my $fh, ">", "data1.txt" ) or die "Can't open > data1.txt: $!" ; for my $i ( 1..1000 ) { print $fh "$i\n" ; } close $fh ; open( $fh, ">", "data2.txt" ) or die "Can't open > data2.txt: $!" ; for my $i ( 1001..2000 ) { print $fh "$i\n" ; } close $fh ; open( $fh, ">", "data3.txt" ) or die "Can't open > data3.txt: $!" ; for my $i ( 2001..3000 ) { print $fh "$i\n" ; } close $fh ;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Merge and split files based on number of lines
by Sekhar Reddy (Acolyte) on Jan 29, 2019 at 23:04 UTC | |
by Veltro (Hermit) on Jan 30, 2019 at 11:08 UTC |