Just for completeness, I should note that the modulo arithmetic in the Perl script I posted was a major brain fart. Here's a hopefully corrected version, with an explanation.
perl -MIO::File -ne 'BEGIN {
@n = ("one.txt", "two.txt", "three.txt", "four.txt");
map { $file[$_] = IO::File->new(">$n[$_]") || die $!} 0..3;
@m = (3, 0, 1, 2, 0, 1, 2, 0, 1, 2);
}
$file[$m[$. % 10]]->print || die $!' filename
I threw in the mapping of arbitrary file names in the array @n for show.
The BEGIN block creates an array @file of four file handles (indexed 0 through 3 -- Perl arrays start at zero) and a mapping @m of which line number to print to which handle. Somewhat confusingly, the first entry in the mapping is for line numbers 10, 20, 30, ... (array index zero), while only the second is for line numbers 1, 11, 21, etc.
In the main loop (outside the BEGIN block) we simply calculate the remainder (modulo) of the line number $. divided by 10 (not 9!!) and use that as an index into @m to get the handle index, and then through another level of indexing print to the handle we are pointed to.
Also for the record, the shell version will have an issue if there is input with backslashes in it. Change read to read -r or if your shell doesn't support that, see if you have the line command instead. Also for maintainability I suppose it would be better to use higher-numbered file descriptors -- file descriptors 1 and 2 are reserved for standard output and standard error, as you probably know. (I wanted to keep them in sync to make the script easier to follow, but it sucks if you try to debug it and lose all your errors into a file someplace.)
As usual, Radoulov's solution is impressive, though a bit hard to follow. Apparently the names of the output files will be the input file name with a number suffix added.
I speculate that mawk keeps the file handles open just in case, i.e. secretly does the file handle juggling that I did explicitly in the Perl script. (Incidentally, you don't really need IO::File for that, but it makes it a lot more readable -- the stuff you have to do to manipulate bare file handles in bare Perl is arcane even by Perl standards.)