#!usr/bin/perl use say; use strict; use warnings; open(my $fh1, ">>", "output1.txt") or die "Can't open > output1.txt: $!"; open(my $fh2, ">>", "output2.txt") or die "Can't open > output2.txt: $!"; my $count = 1; while (<>) { # Read all files that provided through ARGV chomp; if ($count == 1 or $count == 2) { say $fh1 $_; } elsif ($count == 3 or $count == 4) { say $fh2 $_; } if ($count == 4) { $count = 1; next; } $count++; } continue { close ARGV if eof; # reset $. each file } close($fh1) or warn "Can't close output1.txt: $!"; close($fh2) or warn "Can't close output2.txt: $!";