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


 
Thread Tools Search this Thread
Operating Systems Linux Not able to sort a file based on it name. Need your expert comments.
# 1  
Old 12-22-2011
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:
Code:
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:
Code:
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_


Last edited by Scott; 12-22-2011 at 10:42 AM.. Reason: Code tags
# 2  
Old 12-22-2011
Code:
sort -t _ -n -k 7 < input > output

# 3  
Old 12-22-2011
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.
Code:
ls | sort -t _ -n -k 5

# 4  
Old 12-22-2011
Code:
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 ----------

Code:
$ 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_

# 5  
Old 12-22-2011
Thansk itkamaraj & Corona688, it worked. Smilie
# 6  
Old 12-22-2011
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
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 align/sort the column pairs of an csv file, based on keyword word specified in another file?

I have a csv file as shown below, xop_thy 80 avr_njk 50 str_nyu 60 avr_irt 70 str_nhj 60 avr_ngt 50 str_tgt 80 xop_nmg 50 xop_nth 40 cyv_gty 40 cop_thl 40 vir_tyk 80 vir_plo 20 vir_thk 40 ijk_yuc 70 cop_thy 70 ijk_yuc 80 irt_hgt 80 I need to align/sort the csv file based... (7 Replies)
Discussion started by: dineshkumarsrk
7 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 based on positions in flat file

Hello, For example: 12........6789101112..............20212223242526..................50 ( Positions) LName FName DOB (Lastname starts from 1 to 6 , FName from 8 to 15 and date of birth from 21 to29) CURTIS KENNETH ... (5 Replies)
Discussion started by: duplicate
5 Replies

4. 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

5. UNIX for Dummies Questions & Answers

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

Experts I have a list of files in the directory mysample1 mysample2 mysample3 mysample4 mysample5 mysample6 mysample7 mysample8 mysample9 mysample10 mysample11 mysample12 mysample13 mysample14 mysample15 (4 Replies)
Discussion started by: dsedi
4 Replies

6. Shell Programming and Scripting

Help with sort list of file based on similarity

Input file (long list of input file): s_1_1_AABCD.txt s_1_1_ABADA.txt s_1_1_DSCBA.txt s_1_1_DSCCA.txt s_1_1_EATTG.txt s_1_1_FADSD.txt s_1_1_TGACC.txt s_1_1_TTAGG.txt s_1_2_AABCD.txt s_1_2_ABADA.txt s_1_2_DSCBA.txt s_1_2_DSCCA.txt s_1_2_EATTG.txt s_1_2_FADSD.txt ... (1 Reply)
Discussion started by: perl_beginner
1 Replies

7. 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

8. Shell Programming and Scripting

Sort content of text file based on date?

I now have a 230,000+ lines long text file formatted in segments like this: Is there a way to sort this file to have everything in chronological order, based on the date and time in the text? In this example, I would like the result to be: (19 Replies)
Discussion started by: KidCactus
19 Replies

9. Shell Programming and Scripting

Sort file based on column

Hi, My input file is $cat samp 1 siva 1 raja 2 siva 1 siva 2 raja 4 venkat i want sort this name wise...alos need to remove duplicate lines. i am using cat samp|awk '{print $2,$1}'|sort -u it showing raja 1 (3 Replies)
Discussion started by: rsivasan
3 Replies

10. Shell Programming and Scripting

Execution problem with sort the file based on contents

My input file: >ali ASSDDGHFHFHJFJHJDSDGSDGSDGSDGSDGSDGSDGDSGDSGSDGDSGSDGSDGDSGSDGGDSG >zzz ASdASDASDSADSADDSADJKHJDSADKLJADKLSAJDLJLKJLDASDDSADd >abu ASDASDFSAFASFSADFASDASDSADSADSADSADSADSADASDASdSADSADSADA >aaa... (2 Replies)
Discussion started by: patrick87
2 Replies
Login or Register to Ask a Question