Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Splitting a text file

by daxim (Chaplain)
on Mar 19, 2013 at 10:54 UTC ( #1024258=note: print w/replies, xml ) Need Help??

in reply to Splitting a text file

Know your GNU coreutils. split already does what you want. The empty string as last argument suppresses the default prefix, the letter x.

$ split --lines=1000 --numeric-suffixes=1 --suffix-length=5 --addition +al-suffix=NR.txt /the/file ''

Even in Perl, I think you have too much code for so little work. One doesn't need to open the input file oneself, it is simpler to pipe the content from the shell and read from STDIN in Perl. One doesn't need to loop over the input oneself, -n already does that. $. contains the current line number, $_ contains the current line content, see perlvar.

</the/file perl -MIO::File -ne' my $nr = 1+int($./1_000); $handles[$nr] = IO::File->new(sprintf("%05dNR.txt", $nr), "w") unl +ess defined $handles[$nr]; $handles[$nr]->print($_); '

Replies are listed 'Best First'.
Re^2: Splitting a text file
by farang (Chaplain) on Mar 20, 2013 at 17:03 UTC

    Thanks, daxim, it was instructive to me to figure out how the pieces fit together. One thing though, your Perl code as written will put only 999 lines into the first file. This can be fixed by replacing $. with ($.-1)

    </the/file perl -MIO::File -ne' my $nr = 1+int(($.-1)/1_000); $handles[$nr] = IO::File->new(sprintf("%05dNR.txt", $nr), "w") unl +ess defined $handles[$nr]; $handles[$nr]->print($_); '

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1024258]
[Discipulus]: oh and dont miss my last poetry!
[marioroy]: Hello all. After several attempts, am able to move forward and will soon release a Parallel:: ForkManager compatible MCE::Hobo:: Manager, also MCE::Hobo::Simple, and refactored MCE::Hobo engine supporting multiple instances. Wait works beautifully.
[marioroy]: MCE::Shared has been refined that it allows sharing Tie::File and hash objects containing {fh} key.
[marioroy]: It's taken so long to take Hobo to a new level. Threads-like and Parallel:: ForkManager management capaiblities.
[marioroy]: This will complete the 4 years in the making and likely time to move on to something els.

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2017-05-26 08:24 GMT
Find Nodes?
    Voting Booth?