LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-04-2006, 08:34 PM   #1
adamrosspayne
LQ Newbie
 
Registered: Jun 2006
Posts: 19

Rep: Reputation: 0
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 :-)
 
Old 07-04-2006, 08:47 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
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
 
Old 07-04-2006, 09:16 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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.

Last edited by jschiwal; 07-04-2006 at 09:24 PM.
 
Old 07-04-2006, 09:53 PM   #4
adamrosspayne
LQ Newbie
 
Registered: Jun 2006
Posts: 19

Original Poster
Rep: Reputation: 0
thank you :-)

thanks to the both of you. I will be sure and check the man pages more carefully next time. :-)
 
Old 07-04-2006, 11:53 PM   #5
adamrosspayne
LQ Newbie
 
Registered: Jun 2006
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux file extension vs Dos file Extension? manaa Linux - Newbie 6 02-12-2009 05:19 PM
Alphabetical sort wierdness SuperNull Linux - General 2 11-18-2005 01:21 AM
bash script to sort files by extension otheralex Programming 7 08-19-2005 03:40 AM
why when redirecting output to a file any file extension seems to be fine? dr_zayus69 Linux - General 1 05-21-2005 05:09 AM
How can it ell Grip where put my ripped files and sort the in the correct order MvD Linux - Software 2 10-16-2003 04:10 AM


All times are GMT -5. The time now is 04:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration