#!/usr/bin/env perl -l use strict; use warnings; no warnings 'uninitialized'; my @out_records; my $re = qr{ ^ ( .* ) \s ( \d+ ) $ }x; /$re/ and $out_records[$2 - 1] = $1 while ; print for @out_records; __DATA__ Line 5 5 Line 1 1 Line 3 3 #### $ pm_1131675_order_out_by_input_spec.pl Line 1 Line 3 Line 5