list the files with minimum sequence

2020-07-23 09:08发布

问题:

I have some files in a directory as below (not necessarily sorted):

A_10
A_20
A_30
B_10
B_30
C_10
C_20
D_20
D_30
E_10
E_20
E_30

10, 20 and 30 are the sequence numbers of A,B,C,D,E respectively.

I want to select only those files with minimum sequence of all A,B,C,D,E the output should be :

A_10
B_10
C_10
D_20
E_10

could anybody help me?

回答1:

perl -le '
  print join $/, 
    grep !$_{( split "_" )[0]}++, 
      sort glob "*_*"
  '

or:

printf '%s\n' *_* | sort | awk -F_ '!_[$1]++'

or:

printf '%s\n' *_* | sort -t_ -uk1,1


回答2:

In bash:

for x in A B C D E; do
    ls -1 ${x}_* | sort | head -n1
done