Not able to sort a file based on it name. Need your expert comments.

Hi,

I have a files as shown below and I wanted to sort then in following patter based on there names which has "_" in it. I want to sort them according to feild 6th (bold once)value as shown below.

Thanks in advance.

File names:

20111014_manish_STEP2_Files_number__5979-6968_ 
20111014_manish_STEP2_Files_number__6937-7926_ 
20111014_manish_STEP2_Files_number__9918-10907_ 
20111014_manish_STEP2_Files_number__10943-11932_ 
20111014_manish_STEP2_Files_number__59-6968_ 
20111014_manish_STEP2_Files_number__100000-6968_ 
20111014_manish_STEP2_Files_number__7964-8953_ 
20111014_manish_STEP2_Files_number__8981-9970_

After sort it should be like:

20111014_manish_STEP2_Files_number__59-6968_
20111014_manish_STEP2_Files_number__5979-6968_ 
20111014_manish_STEP2_Files_number__6937-7926_ 
20111014_manish_STEP2_Files_number__7964-8953_ 
20111014_manish_STEP2_Files_number__8981-9970_ 
20111014_manish_STEP2_Files_number__9918-10907_ 
20111014_manish_STEP2_Files_number__10943-11932_  
20111014_manish_STEP2_Files_number__100000-6968_
sort -t _ -n -k 7 < input > output

Sorry, but i think i am not able to execute your command correctlly. these are normal files, so what you want me to put in place of input.

I am trying something like this.

ls | sort -t _ -n -k 5
ls -1 | sort -t_ -n -k5

ls -1 ( it is numeric one )

---------- Post updated at 08:52 PM ---------- Previous update was at 08:50 PM ----------

$ ls -1 | sort -n -t_ -k7
20111014_manish_STEP2_Files_number__59-6968_
20111014_manish_STEP2_Files_number__5979-6968_
20111014_manish_STEP2_Files_number__6937-7926_
20111014_manish_STEP2_Files_number__7964-8953_
20111014_manish_STEP2_Files_number__8981-9970_
20111014_manish_STEP2_Files_number__9918-10907_
20111014_manish_STEP2_Files_number__10943-11932_
20111014_manish_STEP2_Files_number__100000-6968_

Thansk itkamaraj & Corona688, it worked. :slight_smile:

To explain what Corona and ItKamaraj did, you have to understand how sort works. If you are not doing it numerically, then 1 comes before 5, which comes before 9, regardless of how many places the actual number goes out to.

If you sort:

90
1000
and 50

they will sort like this

1000
50
90