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

split a file into number of file based on line count

by asha80 (Initiate)
on May 13, 2009 at 06:51 UTC ( #763687=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I have one file with 500 lines in it. i want to split that file into 5 file each should contain 100 lines.

i used delimiter after every hunder lines and i am able to split file into 5 files.

Now what i want is...

Instead of delimiter i want to count number lines in a file.then split that file qually into 5 files..

Please help me in doing this one

spliting file code using delimiter.
#!/usr/bin/perl my $fil_count = 0; my $delim = 'test'; open IN, '< test1.txt' or die "Can't open in.txt: $!\n"; open OUT, '> Out0.txt' or die "Can't write to out0.txt: $!\n"; while (<IN>) { if (/^(.*?)$delim(.*)$/) { print OUT $1 if $1; close OUT; $fil_count++; open OUT, '> Out' . $fil_count . '.txt' or die "Can't write to out +${fil_count}.txt: $!\n"; print OUT $2 if $2; } else { print OUT $_; } } close IN;

Replies are listed 'Best First'.
Re: split a file into number of file based on line count
by moritz (Cardinal) on May 13, 2009 at 06:58 UTC
    If you are on Unix, just use the split command line utility. If you must have a perl solution, simply decide on base of the line counter $. which file to write to.

    Oh, and please put your code examples inside <code>...</code> tags, that way it'll be readable. (Did you use the preview page?)

      more specifically, to split into 5 ~equal files you can use the following construction in bash
      split -l$(expr $(expr $(wc -l filename |cut -d\ -f1) / 5) + 1) filena +me
        Why do all that when you can equally well use
        split -n 100 some_file
        ??

        See man split... (on some *NIXes, -l is used instead of -n to specify the line count)

        Update:

        Qualified the usage of the line count option

        A user level that continues to overstate my experience :-))
        That sounds like it can be golfed.
        split -l$((`cat filename|wc -l`/5+1)) filename
Re: split a file into number of file based on line count
by thiagu_mvt (Sexton) on May 13, 2009 at 07:21 UTC
    In Perl you can read the input file into a array and find the length of the array.(everyline is a scalar now)divide that into 5 and write those into five files one by one in a loop.
Re: split a file into number of file based on line count
by imrags (Monk) on May 13, 2009 at 07:57 UTC
    update: You can use "$." for counting lines.
    my $i =0; open (FILE, "myfile.txt"); while (<FILE>) { ++$i; if($. == 10) { print $_; print "\n Reached\n" } } print $i;#will print number of lines in the file
    You can use an array to store the file and use scalar @arr to get the array size.
    Raghu

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2022-08-19 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?