in reply to Re: Splitting up lines in a few files
in thread Splitting up lines in a few files


The filehandle will be closed when you open it again so you don't have to do that explicitly.

Here is a variation:

perl -ne '$n=$.-1; open F, ">$n.txt" unless $n%5; print F' file Creates: 0.txt 5.txt 10.txt ...

This omits IO checking for the sake of a one-liner.

Here is a slightly longer one but with better output filenames:

perl -ne 'BEGIN{$s="file000"} open F, ">". $s++. ".txt" unless ($. +-1)%5; print F' file Creates: file000.txt file001.txt file002.txt ...

Although, realistically I'd use split for this task as shown by tirwhan above.

--
John.