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.
In reply to split a file by chinni2010
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |