Sort Files based on the number(s) on the file name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort Files based on the number(s) on the file name
# 1  
Old 04-18-2013
Sort Files based on the number(s) on the file name

Experts

I have a list of files in the directory
Code:
mysample1
mysample2
mysample3
mysample4
mysample5
mysample6
mysample7
mysample8
mysample9
mysample10
mysample11
mysample12
mysample13
mysample14
mysample15

I would like to sort them based on the number(s) at the end and so far I have tried
Code:
printf '%s\n' mysample* | sort -t_ -k3n
printf '%s\n' mysample*|sort -t. -k4.2n

but Both sorts the files like the below order
Code:
mysample1
mysample10
mysample11
mysample12
mysample13
mysample14
mysample15
mysample2
mysample3
mysample4
mysample5
mysample6
mysample7
mysample8
mysample9

how should I tweak to get the correct result? ( i.e. 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)

Thanks in advance.

Last edited by vbe; 04-18-2013 at 08:53 AM.. Reason: Please use code tags next time for your data and code
# 2  
Old 04-18-2013
It is not nice but it works

Code:
printf "mysample%d\n" $( ls -1 mysample*|sed 's/mysample//'| sort -n )

This User Gave Thanks to franzpizzo For This Post:
# 3  
Old 04-18-2013
Code:
printf '%s\n' mysample* | sort -k1.9n

Some ls implementations (the one from the GNU coreutils, for example) support the -v option (for natural sort of (version) numbers within text):

Code:
ls -v1 mysample*

For non-interactive use, drop the -1 option.
This User Gave Thanks to radoulov For This Post:
# 4  
Old 04-18-2013
Code:
for i in `cat file`
do
fld1=`echo $i | tr -d '[0-9]'`
fld2=`echo $i | tr -d '[a-z]' |tr -d '[A-Z]'`
echo $fld1 " " $fld2 >> tmp.txt
sort  -n -k2 tmp.txt |awk -F ' ' '{print $1$2}'
rm -f tmp.txt
done

This User Gave Thanks to palanisvr For This Post:
# 5  
Old 04-18-2013
Quote:
Originally Posted by franzpizzo
It is not nice but it works

Code:
printf "mysample%d\n" $( ls -1 mysample*|sed 's/mysample//'| sort -n )

Thank you for the reply.. this is working

---------- Post updated at 09:17 AM ---------- Previous update was at 09:08 AM ----------

Quote:
Originally Posted by radoulov
Code:
printf '%s\n' mysample* | sort -k1.9n

Some ls implementations (the one from the GNU coreutils, for example) support the -v option (for natural sort of (version) numbers within text):

Code:
ls -v1 mysample*

For non-interactive use, drop the -1 option.
Thanks radoulov..
Code:
ls -v1 mysample*

didn't work for us.it said
Code:
ls: Not a recognized flag: v
Usage: ls [-1ACFHLNRSabcdefgiklmnopqrstuxEUX] [File...]

but as exepcted
Code:
printf '%s\n' mysample* | sort -k1.9n

working fine. Thanks for the help.

---------- Post updated at 09:19 AM ---------- Previous update was at 09:17 AM ----------

Quote:
Originally Posted by palanisvr
Code:
for i in `cat file`
do
fld1=`echo $i | tr -d '[0-9]'`
fld2=`echo $i | tr -d '[a-z]' |tr -d '[A-Z]'`
echo $fld1 " " $fld2 >> tmp.txt
sort  -n -k2 tmp.txt |awk -F ' ' '{print $1$2}'
rm -f tmp.txt
done

Thanks Palani.we Will try this.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with number to select based on number

Hi experts, I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number. I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for... (5 Replies)
Discussion started by: mysocks
5 Replies

2. Shell Programming and Scripting

Sort file based on number of delimeters in line

Hi, Need to sort file based on the number of delimeters in the lines. cat testfile /home/oracle/testdb /home /home/oracle/testdb/newdb /home/oracle Here delimeter is "/" expected Output: /home/oracle/testdb/newdb /home/oracle/testdb /home/oracle /home (3 Replies)
Discussion started by: Sumanthsv
3 Replies

3. Shell Programming and Scripting

Sort the file based on number of occurences

I have a file (input) I want to sort the file based on the number of times a pattern in the first column occurs for example grapes occurs 4 times in combination with other patterns so i want it to be first like shown in the output file. then apple ocuurs thrice so it occupies second position and so... (7 Replies)
Discussion started by: anurupa777
7 Replies

4. Shell Programming and Scripting

How to sort the files according to the number?

Hi Everyone, I have a question: I have a lot of file named like or10000.dat, or9100.dat, or100.dat, or3100.dat... I want to deal with these files according to the number in the name. So I want to deal with or100.dat first and then or3100.dat and so on. I used : for i in `ls or*.dat |... (11 Replies)
Discussion started by: wxuyec
11 Replies

5. Shell Programming and Scripting

How to sort files based on file name having numbers

Right now there is no unix direct commad that can sort the files base on its name having numbers: We can use the following: In case your file name are like: abc-UP018.zip xyz-UP019.zip ls *|sort -t'-' -k2 (2 Replies)
Discussion started by: asifansari
2 Replies

6. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

7. Shell Programming and Scripting

Sort and extract based on two files

Hi, I am having trouble sorting one file based on another file. I tried the grep -f function and failed. Basically what I have is two files that look like this: File 1 (the list) gh aba for hmm File 2 ( the file that needs to be sorted) aba 2 4 6 7 for 2 4 7 4... (4 Replies)
Discussion started by: phil_heath
4 Replies

8. Shell Programming and Scripting

Sort and extract based on two files

Hi, I am having trouble sorting one file based on another file. I tried the grep -f function and failed. Basically what I have is two files that look like this: File 1 (the list) gh aba for hmm File 2 ( the file that needs to be sorted) aba 2 4 6 7 for 2 4 7 4 hmm 1 ... (3 Replies)
Discussion started by: phil_heath
3 Replies

9. Shell Programming and Scripting

Split single file into multiple files based on the number in the column

Dear All, I would like to split a file of the following format into multiple files based on the number in the 6th column (numbers 1, 2, 3...): ATOM 1 N GLY A 1 -3.198 27.537 -5.958 1.00 0.00 N ATOM 2 CA GLY A 1 -2.199 28.399 -6.617 1.00 0.00 ... (3 Replies)
Discussion started by: tomasl
3 Replies

10. Shell Programming and Scripting

How to Sort files based on predefined values.?

How to Sort files based on predefined values.? Normally Sorting happens for the alphabetic or numberic orders.. Is there any way to sort a fields based on the Field values..? Field10 has : one two three five four six ten seven eight nine. in predefined order { one, two, three,... (2 Replies)
Discussion started by: p_prathaban
2 Replies
Login or Register to Ask a Question