What's going wrong with sort?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What's going wrong with sort?
# 1  
Old 09-03-2008
What's going wrong with sort?

Sort isn't behaving as I expect. Can someone tell me what's going on?
Incidentally, the infile was ordered by 'sort' previously.
Why is 101m_lig1_frag1 before 101m_lig1_frag12 but after 101m_lig1_frag11?

$infile...
101m_lig1_frag10 124 ...
101m_lig1_frag11 124 ...
101m_lig1_frag12 124 ...
101m_lig1_frag13 124 ...
101m_lig1_frag1 124 ...
101m_lig1_frag14 124 ...
101m_lig1_frag15 124 ...
101m_lig1_frag16 124 ...
101m_lig1_frag17 124 ...
101m_lig1_frag18 124 ...

100> sort $infile > $outfile

$outfile
101m_lig1_frag10 124 ...
101m_lig1_frag11 124 ...
101m_lig1_frag1 124 ...
101m_lig1_frag12 124 ...
101m_lig1_frag13 124 ...
101m_lig1_frag14 124 ...
101m_lig1_frag15 124 ...
101m_lig1_frag16 124 ...
101m_lig1_frag17 124 ...
101m_lig1_frag18 124 ...
# 2  
Old 09-03-2008
use sort -d infile.. Smilie
# 3  
Old 09-03-2008
Thanks, but I'm still getting the same result... wierd!

daisy 1807>cat head.fps | awk '{print $1,$2,"..."}' | sort -d
101m_lig1_frag10 124 ...
101m_lig1_frag11 124 ...
101m_lig1_frag1 124 ...
101m_lig1_frag12 124 ...
101m_lig1_frag13 124 ...
101m_lig1_frag14 124 ...
101m_lig1_frag15 124 ...
101m_lig1_frag16 124 ...
101m_lig1_frag17 124 ...
101m_lig1_frag18 124 ...
# 4  
Old 09-03-2008
sort -n
it may solv ur problem
# 5  
Old 09-03-2008
Quote:
Originally Posted by RahulJoshi
sort -n
it may solv ur problem
Thanks, but I've already tried it. I get the same output as above.
# 6  
Old 09-03-2008
You can give this a try:

Code:
awk '{print substr($1,15)" "$0}' infile | sort -n | cut -d" " -f2-

Regards
# 7  
Old 09-03-2008
Quote:
Originally Posted by Digby
Sort isn't behaving as I expect. Can someone tell me what's going on?
Incidentally, the infile was ordered by 'sort' previously.
Why is 101m_lig1_frag1 before 101m_lig1_frag12 but after 101m_lig1_frag11?

$infile...
101m_lig1_frag10 124 ...
101m_lig1_frag11 124 ...
101m_lig1_frag12 124 ...
101m_lig1_frag13 124 ...
101m_lig1_frag1 124 ...
101m_lig1_frag14 124 ...
101m_lig1_frag15 124 ...
101m_lig1_frag16 124 ...
101m_lig1_frag17 124 ...
101m_lig1_frag18 124 ...

100> sort $infile > $outfile

$outfile
101m_lig1_frag10 124 ...
101m_lig1_frag11 124 ...
101m_lig1_frag1 124 ...
101m_lig1_frag12 124 ...
101m_lig1_frag13 124 ...
101m_lig1_frag14 124 ...
101m_lig1_frag15 124 ...
101m_lig1_frag16 124 ...
101m_lig1_frag17 124 ...
101m_lig1_frag18 124 ...
This is just normal behaviour of sort . You can't do a sort -n as the numbers belongs to the part of string of alphabets.

One option would be:

Code:
for each in $(sed 's/101m_lig1_frag\(.*\) 124\(.*\)/\1/' infile | sort -n); do grep  "^101m_lig1_frag$each " infile; done

Output:

Quote:
101m_lig1_frag1 124 ...
101m_lig1_frag10 124 ...
101m_lig1_frag11 124 ...
101m_lig1_frag12 124 ...
101m_lig1_frag13 124 ...
101m_lig1_frag14 124 ...
101m_lig1_frag15 124 ...
101m_lig1_frag16 124 ...
101m_lig1_frag17 124 ...
101m_lig1_frag18 124 ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use sort to sort numerical column

How to sort the following output based on lowest to highest BE? The following sort does not work. $ sort -t. -k1,1n -k2,2n bfd.txt BE31.116 0s 0s DOWN DAMP BE31.116 0s 0s DOWN DAMP BE31.117 0s 0s ... (7 Replies)
Discussion started by: sand1234
7 Replies

2. UNIX for Beginners Questions & Answers

Difference of Sort -n -k2 -k3 & Sort -n -k2,3

Hi, Could anyone kindly show me a link or explain the difference between sort -n -k2 -k3 & sort -n -k2,3 Also, if I like to remove the row with repetition at both $2 and $3, Can I safely use sort -u -k2 -k3 Example; 100 20 30 100 20 30 So, both $2 and $3 are same and I... (2 Replies)
Discussion started by: Indra2011
2 Replies

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

4. Shell Programming and Scripting

Why result is wrong here ? whether break statement is wrong ?

Hi ! all I am just trying to check range in my datafile pls tell me why its resulting wrong admin@IEEE:~/Desktop$ cat test.txt 0 28.4 5 28.4 10 28.4 15 28.5 20 28.5 25 28.6 30 28.6 35 28.7 40 28.7 45 28.7 50 28.8 55 28.8 60 28.8 65 28.1... (2 Replies)
Discussion started by: Akshay Hegde
2 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

Alternate to sort --random-sort

sort --random-sort The full command is path=`find /testdir -maxdepth 1 -mindepth 1 -type d | ***Some sort of sort function*** | head -1` I have a list I want to randomly sort. It works fine in ubuntu but on a 'osx lion' sort dosen't have the --random-sort option. I don't want to... (5 Replies)
Discussion started by: digitalviking
5 Replies

7. Shell Programming and Scripting

Is it Possible to sort a list of hexadecimal numbers using "sort" command?

Hello Everybody :) !!!. i have question in mind, is it possible to sort a list of hexadecimal numbers using "sort" command? (9 Replies)
Discussion started by: Kesavan
9 Replies

8. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

9. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

10. Shell Programming and Scripting

Sort command giving wrong output

Hi all, I have a problem with sort command. i have a file which looks like this: "file1 1073 java/4 1073 java/180 1073 java/170 1073 java/176 1073 java/167 1073 java/40 1073 java/33 1073 java/136 28988 java/76 28988 java/73 28988 java/48 28988 java/26" and i want to sort... (8 Replies)
Discussion started by: usha rao
8 Replies
Login or Register to Ask a Question