Linux sort
Hi,
I need to sort data numerically by first column and if they are equal then by second column. The columns are separated by spaces and the data looks something like this. -9.21572e-19 0.812056 0.0275 0.812056 -0.0275 0.814485 0.055 0.812056 -0.055 0.820524 0.0825 0.812056 -0.0825 0.832067 -0.085028 0.833333 0.11 0.812056 -0.11 0.848593 -0.120218 0.85625 0.1375 0.812056 Could someone help me out how to write a script for this. I tried the following but it doesnt seem to work. sort -t ' ' -k1,1n -k2,2nr file.txt Thanks, Anks |
When you say "doesn't work", can you be more specific? i.e. tell us what DOES happen.
I wonder if the minus signs are relevant to what is happening? |
From the sort man page:
Quote:
|
This works in perl:
Code:
#!/usr/bin/perl -w Brian |
Just to clarify my previous post (#3): setting the -g switch works.
The edit was about my comment on the number in scientific notation. Using Brian's data: Code:
~/temp: sort -g -k1 -k2 sort.txt |
Hey Norobro,
Thats exactly the kind of output I want but how did you get it to work. sort -g -k1 -k2 sort.txt doesnt help me out. It seems to ignore the negative signs before the numbers. It gives exactly the same output as the numbers in the orginal file without doing any sorting for the file I gave. I am working on mandriva, I am so annoyed that I am caught in something so trivial and my work is stuck. I do not wish to go for a C or perl code when I believe this can be done in one line. Please help me out. Regards, Anks |
sort -n -t' ' sort.txt
I added an additional line in the data to check second column sorting. Code:
cat tempfile |
Code:
sort -n -k1,2 sort.txt |
Quote:
It somehow seems to be ignoring the negative signs while sorting. I tried all the above responses but it doesnt help. Could it be a bug (I am on Mandriva), or some kind of setting needs to be done so that negative signs are not ignored. |
Quote:
Code:
$ cat testfile |
Anks,
As I indicated in my post I used the Brian's (BCarey) data to test for identical first columns. As Colucix pointed out the command can be shortened to: Code:
sort -g -k1,2 foo Just a shot in the dark, but could this be your problem? From the sort man page: Quote:
|
1 Attachment(s)
Hey Guys,
Here is a screenshot of the shell. See if this can be of any help for you guys in finding the problem. Thanks, Anks |
I agree with norobro's suggestion. Maybe your current LOCALE affects the sort order. Please try
Code:
env LC_ALL=C sort -n temp.dat |
Superb Guys,
It works :-) Thanks a lot for all your effors and help. I was really banging my head on my comp for this trivial problem. Anks |
Quote:
Code:
locale Cheers! :) |
All times are GMT -5. The time now is 04:23 AM. |