Sort directory with complex numeric file names


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort directory with complex numeric file names
# 1  
Old 04-15-2009
Sort directory with complex numeric file names

I have a directory with a large number (1000s) of files and I need to produce a file listing all the files in the directory ordered "properly" (properly will be explained shortly).

The files have the following naming pattern:

bul_13_5_228_b.txt
bul_1_3_57.txt
bul_13_6_229.txt
bul_13_6_258.txt
bul_13_6_260.txt
bul_1_3_65.txt
bul_1_3_71.txt
bul_13_7_261.txt
bul_13_7_264.txt

Where field:
1 = "bul" and doesn't change
2 = range from 1 to 30
3 = range from 1 to 12
4 = range from 1 to the 1000s

The sorting pattern I want would sort the 2nd field first, in standard order (1, 2, 3... 10, 11 12...) then the 3rd field (1, 2, 3...) and finally the 4th field.

So, using the file names above as an example, the resulting file would sort like this:

bul_1_3_57.txt
bul_1_3_65.txt
bul_1_3_71.txt
bul_13_5_228_b.txt
bul_13_6_229.txt
bul_13_6_258.txt
bul_13_6_260.txt
bul_13_7_261.txt
bul_13_7_264.txt

I've tried variations of ls | sort and find but cannot seem to get this to work right.

Thanks.
Frank
# 2  
Old 04-15-2009
Try
Code:
ls -1 yourdir | sort -t "_" -n -k2,2 -k3,3 -k4,4

# 3  
Old 04-15-2009
Excellent

Thanks. That worked perfectly. I figured sort was capable of this, but the documentation is sort of lacking.

Best,
Frank

Quote:
Originally Posted by zaxxon
Try
Code:
ls -1 yourdir | sort -t "_" -n -k2,2 -k3,3 -k4,4

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort a text file based on names in square brackets

Hi all, I have a text file similar to this: Text More text Etc Stuff That Is Needed Etc Etc This contains over 70 entries and each entry has several lines of text below the name in square brackets. (5 Replies)
Discussion started by: Scally
5 Replies

2. Shell Programming and Scripting

Sort html based on .jar, .war file names and still keep text within three groups.

Output from zipdiff GNU EAR comparison tool produces output in html divided into three sections "Added, Removed, Changed". I want the output to be sorted by jar or war file. <html> <body> <table> <tr> <td class="diffs" colspan="2">Added </td> </tr> <tr><td> <ul>... (5 Replies)
Discussion started by: kchinnam
5 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Reverse the order of a list of file names (but not sort them alphabetically or numerically)

Hello all, I have a list of file names in a text document where each file name consists of 4 letters and 3 numbers (for example MACR119). There are 48 file names in the document (they are not in alphabetical or numerical order). I would like to reorder the list of names so that the 48th name is... (3 Replies)
Discussion started by: MDeBiasse
3 Replies

4. Shell Programming and Scripting

Compare file names on directory

Dears, Would you please help on following bash script: I want to get the most recent file named alfaYYYYMMDD.gz in one directory: for example: in directory /tmp/ ls -ltr alfa20130715.gz holding.gz alfa20130705.gz sart.txt merge.txt.gz alfa20130802.gz my result shoud be... (1 Reply)
Discussion started by: maxsub
1 Replies

5. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

7. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

8. UNIX for Dummies Questions & Answers

How can I sort the file names in the directory

Hi , I have a list of files in the directory I want to sort based on the file name. But in the middle of filename contains the number based on that I need to sort.Could you suggest me on the same? Example filenames: /user1$ls RS.DEV.ISV.F1.RS.REFDATA.DATA... (1 Reply)
Discussion started by: praveen.thumati
1 Replies

9. UNIX for Dummies Questions & Answers

Change All File Names in a Directory

Hi, If I have a directory full of say 100 random files, and I would like to organize them, for example: FILE001, FILE002, FILE003, FILE004, etc. How would I do this from Terminal, instead of manually changing each file? I'm using Mac OS X, if that makes a difference. Thank you in advance... (8 Replies)
Discussion started by: andou
8 Replies

10. Shell Programming and Scripting

How to sort directory names and use only last?

I need help writing a bash script for the windows cygwin environment. I'm not sure if bash is the optimal tool. Perhaps perl would be better? Either would work. I have directories whose names include the date they were created. The directory names are in the format of... (0 Replies)
Discussion started by: siegfried
0 Replies
Login or Register to Ask a Question