in reply to Splitting up lines in a few files

>perl -ne "BEGIN{open F,'>0.txt'}print F $_;unless($.%5){close F;open +F,'>'.($./5).'.txt';}" file.txt


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: Splitting up lines in a few files
by jmcnamara (Monsignor) on Nov 09, 2005 at 10:12 UTC

    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.