'Need to sort filenames with numeric at Last shell script
Have list of files in the directory as My folder names are as follows,
ATA8/
MUTA11/
USCA12/
MIUATA3/
SCUATA4/
GLUATA1/
HGVOLA10/
MmATA2/
LUATA9/
OLUATA13/
TA14/
LUATA5/
ATA6/
LUA7/
Need to sort files as
GLUATA1
MmATA2
MIUATA3
TA14/
LUATA5/
ATA6/
LUA7
ATA8/
LUATA9
HGVOLA10
MUTA11/
USCA12/
Using shell script
Tried using
ls -d M* | grep -v _old | sort -t "A" -V -k 2
But it is not working as there are Two A
Solution 1:[1]
Suggesting solution with sed and sort:
sed -r "s|^([[:alpha:]]+)([[:digit:]]+)/|\1 \2/|" input.1.txt |sort -n -k 2|sed "s| ||"
results in:
GLUATA1/
MmATA2/
MIUATA3/
SCUATA4/
LUATA5/
ATA6/
LUA7/
ATA8/
LUATA9/
HGVOLA10/
MUTA11/
USCA12/
OLUATA13/
TA14/
Explanation
sed -r "s|^([[:alpha:]]+)([[:digit:]]+)/|\1 \2/|" input.1.txt
Produce: List separated text from numbers with
ATA 8/
MUTA 11/
USCA 12/
MIUATA 3/
SCUATA 4/
GLUATA 1/
HGVOLA 10/
MmATA 2/
LUATA 9/
OLUATA 13/
TA 14/
LUATA 5/
ATA 6/
LUA 7/
sort -n -k 2
Produce: List sorted by second field
GLUATA 1/
MmATA 2/
MIUATA 3/
SCUATA 4/
LUATA 5/
ATA 6/
LUA 7/
ATA 8/
LUATA 9/
HGVOLA 10/
MUTA 11/
USCA 12/
OLUATA 13/
TA 14/
sed "s| ||"
Produce: List removed separator.
GLUATA1/
MmATA2/
MIUATA3/
SCUATA4/
LUATA5/
ATA6/
LUA7/
ATA8/
LUATA9/
HGVOLA10/
MUTA11/
USCA12/
OLUATA13/
TA14/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dudi Boy |
