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