#!/usr/bin/perl -w use strict; use Data::Dumper; my %lines; while () { / \( # find the first ( sign (\d+) # then, capture the number in the brackets /x; $lines{$1} = $_; } #print Dumper %lines; for (sort {$a <=> $b} keys %lines) { print $lines{$_}; } __DATA__ 1opj-A 100.0 M309(67)* F401(159)* Y340(85) 2hyy-A 99.2 F382(147)* M290(55)* M309(67) 2pl0-A 48.3 Y318(88)* F383(159)* 1xbb-A 42.0 L501(134)* A400(38)* 1t46-A 39.4 K623(59)* M309(67)* 2oiq-A 48.9 M314(59)* Y340(85)*