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

use strict; use warnings; my $source ="temp.txt"; my $num = 1; my $line = 10; $line||=1; open(FH,"<$source") or die "Can't open '$source' $!"; while ( my $lines = <FH>) { my $outfile=sprintf("Int_%s.txt",$num); ## Open the file +once every iteration open OUT, ">$outfile"; my $header = sprintf("%-1s%-12s%-8s%-4s%-4s%-10s%-33s","H" +,$outfile,"20101221","1230","$num","$line"," "); print OUT $header ."\n"; ## Prints the header here ### Print the contents of the file here, the header is pri +nted above and trailer is printed below. #### print OUT $lines ; my $trailer = sprintf("%-1s%-12s%-8s%-4s%-4s%-10s%-33s","T +",$outfile,"20101221","1230","$num","$line"," "); print OUT $trailer ."\n"; ## Prints the trailer here $num++ if ($. % $line eq '0'); close (OUT); ## Close the file in the same iteration, so + that a new file is opened next iteration }

Sample temp.txt ----------------- 1234567|HP|GASS 1234567|TP|GASS 3456789|YU|GASS 3245678|TY|GASS o/p: int_1.txt H..... 1234567|HP|GASS 1234567|TP|GASS T....... int_2.txt H..... 3456789|YU|GASS 3245678|TY|GASS T...........

Original node content restored by GrandFather. Note that subsequent edits have been lost.

Replies are listed 'Best First'.
Re: split a file
by GrandFather (Saint) on Dec 23, 2010 at 04:02 UTC

    If you are just interested in a fixed number of lines per record you can:

    use strict; use warnings; my $num = 1; my $recLines = 2; while (!eof DATA) { ## Open the file once every iteration my $outfile = sprintf("Int_%s.txt", $num); print "---> Create file $outfile\n"; printf("%-1s%-12s%-8s%-4s%-4s%-10s%-33s\n", "H", $outfile, "20101221", "1230", $num, $., " "); for (1 .. $recLines) { last if eof DATA; last if ! defined (my $line = <DATA>); print $line; } printf("%-1s%-12s%-8s%-4s%-4s%-10s%-33s\n", "T", $outfile, "20101221", "1230", $num, $., " "); print "<--- Close file $outfile\n"; ++$num; } __DATA__ 1234567|HP|GASS 1234567|TP|GASS 3456789|YU|GASS 3245678|TY|GASS

    Prints:

    ---> Create file Int_1.txt HInt_1.txt 2010122112301 0 + 1234567|HP|GASS 1234567|TP|GASS TInt_1.txt 2010122112301 2 + <--- Close file Int_1.txt ---> Create file Int_2.txt HInt_2.txt 2010122112302 2 + 3456789|YU|GASS 3245678|TY|GASS TInt_2.txt 2010122112302 4 + <--- Close file Int_2.txt

    Note that the sample doesn't include the file creation code, but what you had was ok. However you should always use the three parameter version of open and you should use lexical file handles. Your open should look like:

    # input handle: open my $inFile, '<' $source or die "Can't open '$source' $!"; # output handle: open my $outFile, '>' $target or die "Can't open '$target' $!";
    True laziness is hard work
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: split a file
by rovf (Priest) on Dec 23, 2010 at 09:14 UTC
    Your loop body is executed for each line in the input file.

    Inside the body, you create an output file containing exactly one line. Because of the way your write the "open", any previous file of the same name will be destroyed - you need to open in append mode to keep the old contents, for instance:

    open(my $out,'>>',"Int_$num.txt") || die "Can not open $num-file for a +ppending ($!)";

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: split a file
by k_manimuthu (Monk) on Dec 23, 2010 at 03:58 UTC

    Explain your problem.

    Where you struck?

    What is your expect solution?

      I want to print every 10 records in one file.My new files are having only the evry 10th record from the input file.I guess my Print statement has some problem,need to print in a loop