ascending and descending sort


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ascending and descending sort
# 1  
Old 06-25-2010
Question ascending and descending sort

Hi

I have a problem with sort command :

Code:
sort -nk 1.28,1.34 file | sort -nrk 1.27 file | sort -nk 1.22,1.25 file |sort -nk 1.13,1.21 file | sort -nk 1.9,1.12 file | sort -nk 1.1,1.8 file

This is the input file
Code:
0000000100010000000200004090317003
0000000100010000000230001020592002
0000000100010000000230001090588002
0000000100010000000200000090317003
0000000100010000000130001090588002
0000000200010000000100000090317003
0000000300010000000130001000592002

And the output file is
Code:
0000000100010000000130001090588002
0000000100010000000200000090317003
0000000100010000000200004090317003
0000000100010000000230001020592002
0000000100010000000230001090588002
0000000200010000000100000090317003
0000000300010000000130001000592002

descending sorting is not applied on field 27, and ascending sorting is not applied on field 28-34.

Thanks for your help

Last edited by radoulov; 06-25-2010 at 11:38 AM.. Reason: Please use code tags!
# 2  
Old 06-25-2010
I can't understand your one-liner. But. To get ascending and descending sort order in the same sort:
example
Code:
sort -k1.1,1.10r  -k1.12,1.13 infile > outfile

The trailing "r" reverses the sort sequence for the one field only. In the example the first field. You can use a trailing "n" and many of the other qualifiers to change sort behavior on a per field basis.

also note: sort -k 1.27 means not a single character in the field - it means the field is the whole line from position 27 all the way to the end.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 06-25-2010
I think those successive sorts keep overriding the order that resulted from the prior commands.

Code:
$
$ cat f55
10~123
10~099
10~880
12~451
12~110
09~009
09~101
$
$ sort -t"~" -nk2,2 f55
09~009
10~099
09~101
12~110
10~123
12~451
10~880
$
$ # numeric ascending sort of 2nd field; "~" being the delimiter
$
$ sort -t"~" -nk2,2 f55 | sort -t"~" -nrk1,1
12~451
12~110
10~880
10~123
10~099
09~101
09~009
$
$ # numeric descending sort of 1st field; "~" being the delimiter
$ # but the order resulting from the first sort is lost now
$

Ultimately, all I could figure out is that your data has the first 8 characters sorted in numerically ascending order since that's the last sort command in the pipeline.

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 4  
Old 06-25-2010
MySQL

Yes, There is a problem with successive sorts. I don't know how is the priority is managed in this case.

I changed the sort command like that and it's ok now :

cat "$1" |sort -nk 1.1,1.8 -k 1.9,1.12 -k 1.13,1.21 -k 1.22,1.25 -k 1.28,1.34 -k 1.27r > "$1"_tmp

Thanks for your help
Fafa
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to sort list of directories in descending order in perl?

Hi, I have a problem . I have few directories like inpTDT_1, inpTDT_2, inpTDT_3 and so on inside HOME directory . In one of my perl script (which is in my HOME), the above directories like inpTDT_1, inpTDT_2, inpTDT_3 are sorting out in an order So I wanted to sort all the inpTDT_1, inpTDT_2,... (1 Reply)
Discussion started by: venkatesh
1 Replies

2. Shell Programming and Scripting

Sort date time in ascending order

Hi, i had a data block (coming from pipe from other codes) as: H YF_CO.dat 77164 11/17/2013 04:00:02 731374590.96 1 1 731374590.96 76586 77164 578 2988 Y H YF_CO.dat 77164 11/17/2013 04:00:07 731374590.96 1 4 731374590.96 76586 77164 578 2988 Y H YF_CO.dat 77178 ... (5 Replies)
Discussion started by: pr5439
5 Replies

3. Shell Programming and Scripting

Help with sort data based on descending order problem

Input file 9.99331e-13 8.98451e-65 9.98418e-34 7.98319e-08 365592 111669 74942.9 0 Desired output 365592 111669 74942.9 7.98319e-08 1.99331e-13 6.98418e-34 (2 Replies)
Discussion started by: perl_beginner
2 Replies

4. Programming

Sort ascending n strings in C

Hy guys. My English is not so good, sorry for any mistakes. I'm a bigginer in C, and I have a problem. I want to sort ascending n strings, but I can't read the strings. Here is what I've done so far: //sort ascending n strings #include <stdio.h> int main() { int n,i,j; char a; ... (8 Replies)
Discussion started by: 1/0
8 Replies

5. Linux

Using sort command to get numeric ascending order

HI everyone, I am trying to use the unix sort command to get a list of numbers sorted in ascending order but having trouble in getting it to work. An example of this issue would be when i am trying to sort the following three number each on a different line "1" , "2" and "116" the sort command... (3 Replies)
Discussion started by: wali4813
3 Replies

6. Shell Programming and Scripting

sorting(both Ascending & Descending) files based on multiple fields

Hi All, I am encountered with a problem while sorting a file based on multiple columns . I need to sort like: (field2,ascending) , (field3,ascending) ,(field8,descending) , (field7,ascending),(field13,ascending). So far i was sorting only in ascending order but here i need to use one... (1 Reply)
Discussion started by: apjneeraj
1 Replies

7. UNIX for Dummies Questions & Answers

Sort ascending and descending

How can I sort a file as follows ? cols 1 - 10 ascending cols 11 - 18 descending cols 19 - 20 ascending Thanks (1 Reply)
Discussion started by: don_0110
1 Replies

8. Shell Programming and Scripting

Ascending & Descending order numbers

Dear All, I have below attached file in which i have many nos, i want the last ascending order nos. The brief description is given below. File 315 381 432 315 381 432 315 381 432 315 381 432 315 381 432 (6 Replies)
Discussion started by: pravani1
6 Replies

9. Shell Programming and Scripting

Sort Descending

HI, i want to sort values in descending order and get the column no.s of the sorted value. my data will look like: subject 1 2 3 4 5 bob 78 45 89 99 54 i want the score to be sorted in descending and get the corresponding subject sorted in the output. Please help me with gawk or ??... (1 Reply)
Discussion started by: saint2006
1 Replies

10. UNIX for Dummies Questions & Answers

Sort / ascending order

What's the command to sort a file in ascending order and redirect the output to another file? Thanks!!!!!! (1 Reply)
Discussion started by: gyik
1 Replies
Login or Register to Ask a Question