perl -F\t -anle"($t)=$F[0]=~/^(.{5})/;$t=~s/(.)$/$1<5?0:5/e; $t{$t}=$F[1] if $F[1]>$t{$t} }{ print qq[$_\t$t{$_}] for sort keys %t" file.csv