Sorting file with columns (problem)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sorting file with columns (problem)
# 1  
Old 04-19-2010
Sorting file with columns (solved)

Hi all! I am new to Unix programming so bare with me please Smilie.

I have saved the output of my results in a file called testfile which contains 3 columns a 15 rows.

e.g.
Code:
 175 754 abvd  
 948 454 fewf 
  43 754 fewc
   6 734 feww
  xxx  xxx xxxx

I want to sort the contents of this file in descending order but when I use sort -nr it will only sort the first column while the second column will be sorted accordingly to the sorting of the first, which makes it a bit messy.

Any help will be much appreciated

Last edited by daelas; 04-19-2010 at 11:07 AM.. Reason: solved!
# 2  
Old 04-19-2010
Code:
sort -k1n,2 file

is this what you want?

Last edited by zaxxon; 04-19-2010 at 08:36 AM.. Reason: code tags
# 3  
Old 04-19-2010
Thanks for your quick reply!

It appears it does sort in ascending order correct for both 1 and 2 column but when I try to sort in descending order with

Code:
sort -k1nr,2 file

I still have the same problem Smilie

Last edited by daelas; 04-19-2010 at 09:21 AM..
# 4  
Old 04-19-2010
What about
Code:
sort -k1nr,2 -k2n,3 file

??

Last edited by hergp; 04-19-2010 at 09:20 AM.. Reason: Typo
# 5  
Old 04-19-2010
first colum in descending and secong column in asc

sort -k1,1nr -k2,2n filename
# 6  
Old 04-19-2010
I still have the same problem. I want both 1st and 2nd column to be in descending order.
I have this
Code:
353 202
313 177 
336 188 
367 209 
341 195 
201 137 
345 188 
123 99 
153 117 
1245 390 
188 132 
145 112 
185 127 
162 129 
168 122

and after everything I've tried it sorts like this :

Code:
1245 390
367 209 
353 202 
345 188 
341 195  <-- error
336 188 
313 177 
201 137 
188 132 
185 127 
168 122 
162 129 
153 117 
145 112 
123 99

# 7  
Old 04-19-2010
This is not an error, because sort works hierarchically. The second sort-field is only examined, if there are non unique values in the first field. 341 in the first field is less then 345 in the first field of the previous line and greater then 366 in the next line, so 195 ist not used for anything in the sort process.

Do you by any chance want to sort the two columns independently (that would break up records)?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting by columns

Hi, I have a tab delimited columnar file where I want to remove lines wherever two particular columns match. so for this file, I want to toss the lines where columns 1 and 2 match: a a 1 3 a b 2 4 b b 3 5 because there are matches column 1 and 2 in lines 1 and 3, I would like a script to... (2 Replies)
Discussion started by: mikey11415
2 Replies

2. UNIX Desktop Questions & Answers

Problem in sorting a text file

Hi; I have a text file like this: 1 10 11 2 3 4 M X Y When I sort it numerically using sort -n, it looks like this: Y X M 1 2 3 4 10 (3 Replies)
Discussion started by: a_bahreini
3 Replies

3. Shell Programming and Scripting

sorting and adding columns

i have a file with two columns, and i want to uniquely sort the values in fist column and add the corresponding values in the second columns eg file a contents tom 200 john 300 sow 500 tom 800 james 50 sow 300 output shpould be in file b as tom 1000 john 300 sow 800 james 50 (0 Replies)
Discussion started by: dealerso
0 Replies

4. Shell Programming and Scripting

Chemistry problem- File matching and Sorting!!!

Dear Programmers I have a file called ranking.txt, in which I have 4 chemical compounds in *.sdf file format named ligands_m1, ligands_m2, ligands_m3, ligands_m4. Each compounds is assigned with a particular score along with the file location. ... (0 Replies)
Discussion started by: robertselwyne
0 Replies

5. Shell Programming and Scripting

Transposing column to row, joining with another file, then sorting columns

Hello! I am very new to Linux and I do not know where to begin... I have a column with >64,000 elements (that are not in numberical order) like this: name 2 5 9 . . . 64,000 I would like to transpose this column into a row that will later become the header of a very large file... (2 Replies)
Discussion started by: doobedoo
2 Replies

6. UNIX for Dummies Questions & Answers

Vertical sorting of columns

Please help to sort columns in this file: a b d f c e 1 4 10 16 7 13 2 5 11 17 8 14 3 6 12 18 9 15 I need to sort COLUMNS (so sort command doesn't work) like this: a b c d e f 1 4 7 10 13 16 2 5 8 11 14 17 3 6 9 12 15 18 I know sed can do it but don't know how... :( (7 Replies)
Discussion started by: coppuca
7 Replies

7. Shell Programming and Scripting

sorting file based on two or more columns

Hi gang. I'm using a unix/mac system and i'm trying to sort a file (more than 1,000,000 lines). chr1 100000965 100001001 - chr1 100002155 100002191 + chr1 100002165 100002201 + chr1 100002525 100002561 - chr1 10000364 ... (2 Replies)
Discussion started by: labrazil
2 Replies

8. UNIX for Dummies Questions & Answers

sorting file content on columns

guys i have a question: i'd like to sort files (as many I want) in columns so to visualize them one near the other...so let's say i have just 2 files: FILE1 John Mary Bridget FILE2 Anne Robert Mark i would like to obtain: John Anne Mary Robert Bridget ... (2 Replies)
Discussion started by: marshmallow
2 Replies

9. UNIX for Dummies Questions & Answers

sorting a file with multiple columns

I have a file with several columns: Column1 Column2 Column3. . .Column6 I want to sort the data from Column6. Could I do that through sort even if there are spaces in between fields? Much thanks! outta. (3 Replies)
Discussion started by: outtacontrol
3 Replies

10. Shell Programming and Scripting

Problem in sorting the file

file format is word filename no.of occurances ------------------------------ wish f3.txt 2 wish f2.txt 1 cold f1.txt 5 cold f2.txt 3 cold f1.txt 3 cold e.txt 3 gold f1.txt 7 gold f3.txt 3 rush e.txt 2 itz abt building a search index for every word in the files given as input the... (1 Reply)
Discussion started by: vishnu_vaka
1 Replies
Login or Register to Ask a Question