use strict; use warnings; my %hash; open (my $inFile, '<', shift) or die "$!"; open (my $outFile, '>', shift) or die "$!"; binmode($inFile); binmode($outFile); while (<$inFile> =~ /(\d)\s+(\d)\s+(\d)\s+(\w+)/){ push @{$hash{$4}}, $1, $2, $3; } print $outFile "@{$hash{$_}}[0..2] $_\n" for sort keys %hash; #### Usage: C:\path\to\script\perl my_script.pl inFile.txt outFile.txt