LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to 'sort' by file extension then alphabetical order (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-sort-by-file-extension-then-alphabetical-order-461035/)

adamrosspayne 07-04-2006 07:34 PM

how to 'sort' by file extension then alphabetical order
 
Hi helpful people, i have a text file that lists hundreds of files, one per line. e.g:

ccc.avi
bbb.doc
bbb.avi
bbb.mp3
ccc.doc
aaa.mp3

How can i sort this file, firstly by extension type, and for each group of matching extensions, into alphabetical order? e.g:

bbb.avi
ccc.avi

bbb.doc
ccc.doc

aaa.mp3
bbb.mp3


cheers :-)

gilead 07-04-2006 07:47 PM

It's a good idea to check the man page before posting. For example, man ls lists the -X switch:
Code:

~/tmp$ ls -l
total 0
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 aaa.mp3
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 bbb.avi
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 bbb.doc
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 bbb.mp3
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 ccc.avi
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 ccc.doc
steve@qh10222499:~/tmp$ ls -lX
total 0
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 bbb.avi
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 ccc.avi
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 bbb.doc
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 ccc.doc
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 aaa.mp3
-rw-r--r-- 1 steve steve 0 2006-07-05 10:41 bbb.mp3


jschiwal 07-04-2006 08:16 PM

Please see the "man sort" manpage. If you make the decimal point the field separator, and sort by the second field, you will get the results you want.

This will work for 8.3 filenames, or other filenames that contain only one decimal point in the name.
Your locale setting can effect how things are sorted. For some locale settings 'a'=='A'. For others 'a' < 'A'. The locale settings can also effect grep and sed. I discovered this when [[:lower:]] was selecting upper case letters in a regular expression.

By the way, there is also a "man regex" man page.

adamrosspayne 07-04-2006 08:53 PM

thank you :-)
 
thanks to the both of you. I will be sure and check the man pages more carefully next time. :-)

adamrosspayne 07-04-2006 10:53 PM

They were right...
 
using the below 'sort' options as i think i now understand them (thanks helpers), the -t (something) allows one to designate what string will be used to seperate a larger string into fields either side of it. The --key=2 translates to 'use the field following the separator string to sort by'. So;

sort -t . --key=2

worked ok. Noted only when file follow the blarty.bla format and not bar.ty.bla


All times are GMT -5. The time now is 11:27 PM.