#!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: $!"; while (<>) { # Read all files that provided through ARGV chomp; if ($. == 1 or $. == 2) { say $fh1 $_; } elsif ($. == 3 or $. == 4) { say $fh2 $_; } $. = 0 if ($. == 4); } 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: $!";