#! perl -sw use strict; my $file = $ARGV[0]; my( $prefix, $suffix ) = ( $file =~ m[^(.+)\.([^.]+)] ); open I, '<', $file or die $!; my @outs; open $outs[ $_ ], '>', "$prefix.${_}.$suffix" or die $! for 1 .. 4; my $out = 0; while( ) { print { $outs[ $out+1 ] } $_; if( /^5/ ) { ++$out; $out %= 4; } }