Numerically sort problem for a long list of file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Numerically sort problem for a long list of file name
# 1  
Old 11-18-2009
Numerically sort problem for a long list of file name

I got a long list of file name.
My input:
data_1.txt
data_2.txt
data_3.txt
data_10.txt
data_21.txt
data_12.txt
data_4.txt

My desired output:
data_1.txt
data_2.txt
data_3.txt
data_4.txt
data_10.txt
data_12.txt
data_21.txt

Does anybody got idea how to archive it?
I got try this code:
Code:
 
ls *.txt | sort -n

But it give the output like this:
data_1.txt
data_10.txt
data_12.txt
data_2.txt
data_21.txt
data_3.txt
data_4.txt

Which is not what I want Smilie
Thanks a lot for suggestion.
# 2  
Old 11-18-2009
If you can put all filenames in a file, try following command to get that file contents sorted.
sort -k 1,1n f1
# 3  
Old 11-18-2009
sad to said that it can't work Smilie
It give something like this:
data_10.txt
data_12.txt
data_1.txt
data_21.txt
data_2.txt
data_3.txt
data_4.txt

Which is not what I desired Smilie
Still thanks for your help.
# 4  
Old 11-18-2009
Try:
Code:
sort -t_ -k2n

# 5  
Old 11-18-2009
Hi, it come out something like this as well:
data_1.txt
data_10.txt
data_12.txt
data_2.txt
data_21.txt
data_3.txt
data_4.txt

Do you have better suggestion?
Thanks again.
# 6  
Old 11-18-2009
Are you sure? this command should work.
Code:
$ sort -t_ -k2n urfile
data_1.txt
data_2.txt
data_3.txt
data_4.txt
data_10.txt
data_12.txt
data_21.txt

# 7  
Old 11-18-2009
Hi, you are right. It is worked.
But I feel quite weird that why it can't work if my content file name is like this:
amino_acids_10.txt
amino_acids_12.txt
amino_acids_21.txt
amino_acids_1.txt
amino_acids_2.txt
amino_acids_3.txt
amino_acids_4.txt

Do you know what is the reason?
I feel quite weird and confusing why the code only work for certain file content?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to sort files in directory numerically?

Trying to sort a bunch of files numerically but can't seem to get the command just right. This is in a IBM AIX machine. I have a directory that has... backup.bk1 backup.bk100 backup.bk2 backup.bk200 backup.bk3 backup.bk300 There are a lot more files but this is shortened for the... (5 Replies)
Discussion started by: c3rb3rus
5 Replies

2. Shell Programming and Scripting

List numerically in ascending order

Hello, I am running ubuntu 16.04 and trying to list all files inside a directory, I need to sort them in ascending order. While surfing on the site, I found an old thread but somehow it did not work. Link Ascending order with sort -nk2 myfile.txt command gives below output: file... (5 Replies)
Discussion started by: baris35
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

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

5. Shell Programming and Scripting

Argument list too long problem

I have a huge set of files (with extension .common) in my directory around 2 million. When I run this script on my Linux with BASH, I get /bin/awk: Argument list too long awk -F'\t' ' NR == FNR { a=NR } NR != FNR { sub(".common", "", FILENAME) print a, FILENAME, $1 } '... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Shell Programming and Scripting

Help with cat long list of file

I have long list of input file's content that I plan to "cat" all of the content into another output file. The total input file is around 20,000 which all named with ".txt" Below is the command that I try: cat *.txt > all_file.out -bash: /usr/bin/sudo: Cannot allocate memory Unfortunately,... (2 Replies)
Discussion started by: perl_beginner
2 Replies

7. Shell Programming and Scripting

Sort alphabetically, then numerically

Greetings - I'm not necessarily new to bash scripting - I'm probably between beginner and intermediate, but I have something that I just cannot figure out after many attempts to find it. I have a file that is merely a list of many files, with their respective paths, and a branch path (ClearCase)... (5 Replies)
Discussion started by: 1cor29
5 Replies

8. Shell Programming and Scripting

Sort numerically a non numerical

Hello, I have this sample data: 01 * * * * 01 * * * * 01 * * * * 01 * * * * 01 0 * * * 01 0 * * * 01 0 * * * 01 0 * * * 02 * * * 0 02 * * * 0 02 * * * 6 02 * * * 6 02 0 * * 1 02 0 * * 1 02 0 * * 2 02 0 * * 2 02 0 * * 3 (3 Replies)
Discussion started by: gio001
3 Replies

9. UNIX for Dummies Questions & Answers

Sort 2 columns numerically

Hi, A basic query. In the example file below, I want to sort by column 1 and then by column 2 numerically. I have tried sort -k2n,1 file1 but while this sorts the columns in the correct order, it does not sort column 2 numerically. Any help would be much appreciated. Also, if you have time to... (3 Replies)
Discussion started by: auburn
3 Replies

10. UNIX for Dummies Questions & Answers

Sort file alphabetically AND numerically

Hi all. I have 2 files like this: f1 A 10 B 80 C 9 f2 A 11 B 700 C 10 What I want is the concatenation of the two files sorted by name (alphabetically) and size (numerically), so the result should be like this: F3 (cat f1 f2 sorted) A 10 A 11 B 80 B 700 (2 Replies)
Discussion started by: mrodrig
2 Replies
Login or Register to Ask a Question