#!/usr/bin/perl -w use strict; my %hash; while () { chomp; next unless /\S/; # Skip blank lines /(\d+)/; # Match the first number $hash{$1} = $_; } for my $key (sort {$a <=> $b} keys %hash) { print $key, "\t", $hash{$key}, "\n"; } # Prints: # 1 1 2 3 4 5 # 10 10 20 30 40 50 # 200 200 100 300 2 1 __DATA__ 1 2 3 4 5 10 20 30 40 50 200 100 300 2 1