LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Linux sort (https://www.linuxquestions.org/questions/linux-newbie-8/linux-sort-702056/)

ankursinha 02-03-2009 06:06 PM

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

pixellany 02-03-2009 07:02 PM

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?

norobro 02-03-2009 07:23 PM

From the sort man page:
Quote:

-g, --general-numeric-sort : compare according to general numerical value
I think the number in scientific notation will be a problem though. [ EDIT: Never mind. Posted before trying it]

BCarey 02-03-2009 08:13 PM

This works in perl:

Code:

#!/usr/bin/perl -w
#
use strict;

my @unsorted;

for (<DATA>) {
        next if /^\s$/;
        push @unsorted, [split /\s/];
}

my @sorted = sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1]} @unsorted;

for (@sorted) {
        print "$_->[0]  $_->[1]\n";
}

__DATA__
-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

I slightly changed your data so you would see that it works if the first value is equal.

Brian

norobro 02-03-2009 09:16 PM

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
-0.120218 0.85625
-0.11 0.848593
-0.085028 0.833333
-0.0825 0.832067
-0.0275 0.814485
-9.21572e-19 0.812056
0.0275 0.812056
0.055 0.812056
0.055 0.820524
0.0825 0.812056
0.11 0.812056
0.1375 0.812056


ankursinha 02-04-2009 02:36 AM

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

jschiwal 02-04-2009 02:52 AM

sort -n -t' ' sort.txt

I added an additional line in the data to check second column sorting.
Code:

cat tempfile
-9.21572e-19 0.812056
0.0275 0.812056
-0.0275 0.814485
0.0275 0.110
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
[jschiwal@hpamd64 ~]$ sort -n -t' ' tempfile
-9.21572e-19 0.812056
-0.120218 0.85625
-0.11 0.848593
-0.085028 0.833333
-0.0825 0.832067
-0.055 0.820524
-0.0275 0.814485
0.0275 0.110
0.0275 0.812056
0.055 0.812056
0.0825 0.812056
0.11 0.812056
0.1375 0.812056

But if you use scientific notation, you may need to use a generic sort (-g) instead.

colucix 02-04-2009 02:54 AM

Code:

sort -n -k1,2 sort.txt
Edit: jschiwal beat me! ;)

ankursinha 02-04-2009 04:27 AM

Quote:

Originally Posted by pixellany (Post 3431118)
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?

pixellany, you got it right, it has to do something with the minus sign...if I keep all the numbers positive it is working well.

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.

colucix 02-04-2009 07:20 AM

Quote:

Originally Posted by ankursinha (Post 3431597)
I tried all the above responses but it doesnt help.

Can you elaborate, please? Copy and paste what do you see in your terminal, otherwise we cannot be of much help. The following is a working example showing the input, the command, the output:
Code:

$ cat testfile
-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
$ sort -n testfile
-9.21572e-19 0.812056
-0.120218    0.85625
-0.11        0.848593
-0.085028    0.833333
-0.0825      0.832067
-0.055      0.820524
-0.0275      0.814485
 0.0275      0.812056
 0.055      0.812056
 0.0825      0.812056
 0.11        0.812056
 0.1375      0.812056


norobro 02-04-2009 09:27 AM

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
We have a pretty good sampling of distros in this thread where the sort command works. I can't imagine that Mandriva would be any different.

Just a shot in the dark, but could this be your problem? From the sort man page:
Quote:

*** WARNING *** The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional
sort order that uses native byte values.

ankursinha 02-05-2009 03:20 PM

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

colucix 02-05-2009 04:51 PM

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

ankursinha 02-06-2009 05:09 AM

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

colucix 02-06-2009 05:58 AM

Quote:

Originally Posted by ankursinha (Post 3433980)
Superb Guys,

It works :-)

Nice to hear the good news! For your information, you can use the command
Code:

locale
to see the current settings based on the language you chose for your computer. They affect the behaviour of many different commands. Even a simple ls can sort entries in a different way if you change your current locale settings. Giving a different environment to some specific commands, using the env method as suggested above, is a very common task in order to prevent strange or unexpected behaviours.

Cheers! :)


All times are GMT -5. The time now is 04:23 AM.