Sorting files based upon the numeric value before the first field separator
Lets say i have a list of files.for eg:
ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
CD8_PKG_CV.txt
ABCD1_11.txt
BCD2_fbefjf.txt
D3_C.txt
ABCD4_123.txt
ABCD10_123.txt
I want to sort these files according to the first number before the first underscore.
I tried with sort but with no proper solution then i tried it in perl and this worked.Hopefully the below helps.
ls -1 | perl -F"_" -lane '$F[0]=~s/[a-zA-Z]//g;$X{$F[0]}=$_;END{foreach(sort {$a<=>$b} keys(%X)){print $X{$_}}}'
tested below:
> ls -1
ABCD10_123.txt
ABCD1_11.txt
ABCD4_123.txt
ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
BCD2_fbefjf.txt
CD8_PKG_CV.txt
D3_C.txt
> ls -1 | perl -F"_" -lane '$F[0]=~s/[a-zA-Z]//g;$X{$F[0]}=$_;END{foreach(sort {$a<=>$b} keys(%X)){print $X{$_}}}'
ABCD1_11.txt
BCD2_fbefjf.txt
D3_C.txt
ABCD4_123.txt
ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
CD8_PKG_CV.txt
ABCD10_123.txt
>
0 comments: