Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Split files into chunks of $n lines

by ktsirig (Sexton)
on Sep 27, 2005 at 15:50 UTC ( [id://495420]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all! I am working a while with Perl, nut I still have some difficulties. My current problem is the following: I have a file, which I want to open and 'split' it in several files, each of them containing 4000 lines. For example, if my initial file has 6200 lines, there will be 2 smaller files created, the first containing 4000 lines and the second 2200 lines. Is there any quick way to do this? I must also point out that this script will be executed in batch mode, that is I will find each file and, by using xargs, I will pass each 'big' file in the perl script, which will then open the file etc Any hints??? Thanx

2005-09-28 Edit by castaway -
Original title: 'a simple (?) question'

Replies are listed 'Best First'.
Re: Split files into chunks of $n lines
by kirbyk (Friar) on Sep 27, 2005 at 15:56 UTC
    If you're in unix, use the 'split' command. I think the syntax would be 'split -l 4000 filename', and it will do exactly what you want. (See man split for more detail.)

    It's not tremendously hard to do it through perl, and if you want to modify anything that'd be useful. I'm sure someone will post code for that very soon. There's more than one way to do it!

    -- Kirby, WhitePages.com

      Even if you don't use Unix (or do but are missing the split command), you can download a pure-Perl implementation of split from the Perl Power Tools site. :)

          --k.


Re: Split files into chunks of $n lines
by Anonymous Monk on Sep 27, 2005 at 16:26 UTC
    Untested:
    use strict; use warnings; die "Usage $0 from to" unless @ARGV == 2; my $to = pop @ARGV; $to .= ".%03d"; my $count = 1; my $fh; while (<>) { if ($. % 4000 == 1) { unless ($. == 1) { close $fh or die; } open $fh, ">", sprintf $to, $count ++ or die; } print $fh $_; } close $fh or die;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-18 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found