Com o recente (> 4.0) GNU awk, usando um array associativo digitado no campo numérico (do penúltimo ao último):
printf '%sprintf '%sprintf '%sprintf '%sprintf '%sprintf '%s%pre%' * | perl -F'[_.]' -0ne '
$h{$F[$#F-1]} = $_ }{ for $k (sort { $a <=> $b } keys %h) {print "$h{$k}\n"}
'
' * | gawk 'BEGIN{RS="%pre%0"; FS="[_.]"; PROCINFO["sorted_in"]="@ind_num_asc"} {a[$(NF-1)]=$0} END {for (k in a) print a[k]}'
n2+_PiU_w1_5348696.out
n2+_PiU_w1_5348729.out
n2+_PiU_w1_5348742.out
n2+_PiU_w2_5348755.out
n2+_PiU_w4_5348757.out
n2+_PiU_st3_w3_part5_5630720.out
n2+_PiU_st3_w3_part6_5630721.out
n2+_PiU_st3_w3_part4_5630813.out
n2+_PiU_st3_w3_part6_5630814.out
' * | gawk '
BEGIN {
RS="%pre%0"; FS="[_.]";
PROCINFO["sorted_in"]="@ind_num_asc"
}
{
a[$(NF-1)]=$0
}
END {
for (k in a) print a[k]
}'
' * | perl -F'[_.]' -0ne '
$h{$F[$#F-1]} = $_ }{ for $k (sort { $a <=> $b } keys %h) {print "$h{$k}\n"}
'
' * | gawk 'BEGIN{RS="%pre%0"; FS="[_.]"; PROCINFO["sorted_in"]="@ind_num_asc"} {a[$(NF-1)]=$0} END {for (k in a) print a[k]}'
n2+_PiU_w1_5348696.out
n2+_PiU_w1_5348729.out
n2+_PiU_w1_5348742.out
n2+_PiU_w2_5348755.out
n2+_PiU_w4_5348757.out
n2+_PiU_st3_w3_part5_5630720.out
n2+_PiU_st3_w3_part6_5630721.out
n2+_PiU_st3_w3_part4_5630813.out
n2+_PiU_st3_w3_part6_5630814.out
' * | gawk '
BEGIN {
RS="%pre%0"; FS="[_.]";
PROCINFO["sorted_in"]="@ind_num_asc"
}
{
a[$(NF-1)]=$0
}
END {
for (k in a) print a[k]
}'
ex.
%pre%
Da mesma forma, usando um hash perl:
%pre%