Inconsistent behavior when sorting by column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inconsistent behavior when sorting by column
# 1  
Old 11-22-2014
Inconsistent behavior when sorting by column

I'm trying to sort the list below, so that multiple instances of the form
pass=i_d=j

are contiguous. Curiously, the standard sort command to do this works some of the time (it produced the output below) but not all of the time.

the command I used was

Code:
cat fileName | sort -k1.26,1.26n -k1.29,1.29n

where the contents of fileName are below.

this command produced 3 consecutive lines with pass=2_d=2 and 3 with pass=2_d=3, as intended, but then lower down the file, there are, for example 3 lines with pass=3_d=2, but separated by a line with pass=3_d=3.
That is, the ordering of the 3rd and 4th lines from the bottom should be reversed.

Could anybody please explain why I'm getting this inconsistent sort behavior?

Thanks very much for any help.


Code:
03/getBasisSet_n=03_pass=0_d=1_cov=0.050_dg=17_mthd=nIP.mat
05/getBasisSet_n=05_pass=0_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=0_d=3_cov=0.050_dg=17_mthd=nIP.mat
09/getBasisSet_n=09_pass=0_d=4_cov=0.050_dg=17_mthd=nIP.mat
11/getBasisSet_n=11_pass=0_d=5_cov=0.050_dg=17_mthd=nIP.mat
04/getBasisSet_n=04_pass=1_d=1_cov=0.050_dg=17_mthd=nIP.mat
05/getBasisSet_n=05_pass=1_d=2_cov=0.050_dg=17_mthd=nIP.mat
06/getBasisSet_n=06_pass=1_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=1_d=3_cov=0.050_dg=17_mthd=nIP.mat
08/getBasisSet_n=08_pass=1_d=3_cov=0.050_dg=17_mthd=nIP.mat
05/getBasisSet_n=05_pass=2_d=1_cov=0.050_dg=17_mthd=nIP.mat
05/getBasisSet_n=05_pass=2_d=2_cov=0.050_dg=17_mthd=nIP.mat
06/getBasisSet_n=06_pass=2_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=2_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=2_d=3_cov=0.050_dg=17_mthd=nIP.mat
08/getBasisSet_n=08_pass=2_d=3_cov=0.050_dg=17_mthd=nIP.mat
09/getBasisSet_n=09_pass=2_d=3_cov=0.050_dg=17_mthd=nIP.mat
06/getBasisSet_n=06_pass=3_d=1_cov=0.050_dg=17_mthd=nIP.mat
06/getBasisSet_n=06_pass=3_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=3_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=3_d=3_cov=0.050_dg=17_mthd=nIP.mat
08/getBasisSet_n=08_pass=3_d=2_cov=0.050_dg=17_mthd=nIP.mat
08/getBasisSet_n=08_pass=3_d=3_cov=0.050_dg=17_mthd=nIP.mat
09/getBasisSet_n=09_pass=3_d=3_cov=0.050_dg=17_mthd=nIP.mat

Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output data as well as for code snippets.

Last edited by Don Cragun; 11-22-2014 at 06:39 AM.. Reason: Add CODE tags and ICODE tags.
# 2  
Old 11-22-2014
Presumably you wanted to sort on i and j in pass=i_d=j; but the command:
Code:
cat fileName | sort -k1.26,1.26n -k1.29,1.29n

is sorting on i and the equal sign (and is slowing you down by reading and writing the contents of your input file an additional time).

Try:
Code:
sort -k1.26,1.26n -k1.30,1.30n fileName

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 11-22-2014
And, with one single letter to sort on, the n option doesn't hurt but is redundant.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-22-2014
What a stupid mistake!
Thanks to RudiC and Don.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting a specific column!

What im trying to do is sort the output by the number on the second column and than limit the result to only the first three lines. This is the code idlist="x23s52; f34233; 2343xs; 25x34; si342d" cntr=1 idcnt=$(print $nidlist |tr ';' '\n' |wc -l) numofgrps=0 while (($cntr <= $idcnt))... (3 Replies)
Discussion started by: ajetangay
3 Replies

2. Shell Programming and Scripting

Sorting unique by column

I am trying to sort, do uniq by 1st column and report this 4 columns tab delimiter table , eg chr10:112174128 rs2255141 2E-10 Cholesterol, total chr10:112174128 rs2255141 7E-16 LDL chr10:17218291 rs10904908 3E-11 HDL Cholesterol chr10:17218291 rs970548 8E-9 TG... (4 Replies)
Discussion started by: fat
4 Replies

3. UNIX for Dummies Questions & Answers

Independent column sorting

Dear All, I have a set of columns (usually unknown number) and I would like to sort each of them (descending values) but my columns are independent. Can you please help me file1 1,5,7 3,4,9 2,6,8 outcome should be 1,4,7 2,5,8 3,6,9 I have tried sort -n -k1,1 -k2,2 -k3,3 but this... (8 Replies)
Discussion started by: A-V
8 Replies

4. UNIX for Dummies Questions & Answers

Sorting one column underneath another

I am trying to arrange these columns so that they look like this: Xray2_1255555 Number of Copies: 1 Boxcar_1387305895 Number of Copies: 2 Fox_1387305896 Number of Copies: 2 But I have one column after another like this: Xray2_1255555 Number of Copies: 1 Xray2_12444444 Number of... (5 Replies)
Discussion started by: newbie2010
5 Replies

5. Shell Programming and Scripting

Inconsistent column printing

Hi, I have a file that has inconsistently numbered columns. Like row1 has 23 columns, and row 2 has 34 columns etc. I would like to re-order the first 8 columns as required and from the 9th column till the end, I would like to print it as it is. I tried to read the re-ordered 8 columns... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

6. Shell Programming and Scripting

[Solved] Sorting a column based on another column

hello, I have a file as follows: F0100010 A C F0100040 A G BTA-28763-no-rs 77.2692 F0100020 A G F0100030 A T BTA-29334-no-rs 11.4989 F0100030 A T F0100020 A G BTA-29515-no-rs 127.006 F0100040 A G F0100010 A C BTA-29644-no-rs 7.29827 F0100050 A... (9 Replies)
Discussion started by: Homa
9 Replies

7. Shell Programming and Scripting

sorting on one column

Hello all, Is there a way to sort only one column while keeping everything else intact. Take for example this situation: (all columns are space separated) 11 AA asdf 1 -0.5 xx 11 AA axdf 1 -0.6 xx 11 AA csls 1 -0.7 xx 11 AA hjkj 1 -0.4 xx 11 AA uius 1 -0.8 xx 22 AA asdf 1 -0.4 xx 22 AA... (7 Replies)
Discussion started by: jaysean
7 Replies

8. Shell Programming and Scripting

Sorting a particular column in PERL

I have a file abc.txt which contains data in th following format abc,23 hgfh,87 tweg,89 jdfjn,74 I want to sort on the basis of column (the second one). It should be numerical sort. output should be abc,23 jdfjn,74 hgfh,87 tweg,89 I know how to do it in unix. I need a PERL code (1 Reply)
Discussion started by: centurion_13
1 Replies

9. Shell Programming and Scripting

Sorting By Column

I have almost got my unix program working that im working on as a personal project, my file tvs.txt has around 500 records so this would make it alot easier for me to find specific information. I have a file called tvs.txt, I'm using pico to edit the files and i want to be able to sort on the a... (6 Replies)
Discussion started by: john123
6 Replies

10. Shell Programming and Scripting

Sorting a column in this scenario

Hi, I need to sort the 3rd column in ascending order , each and every column are seperated by ~. sample input file : 2~Lead Time Metrics~jennife1 2~Lead Time Metrics~mmullis 2~Lead Time Metrics~lisah 2~Lead Time Metrics~pros 2~Lead Time Metrics~kenward can any one help me out . i... (1 Reply)
Discussion started by: krishnan_6015@y
1 Replies
Login or Register to Ask a Question