Find the answer to your Linux question:
Results 1 to 9 of 9
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 ...
  1. #1
    Just Joined!
    Join Date
    Jan 2009
    Location
    Helsinki, Finland
    Posts
    8

    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,2n file.txt

    Thanks,
    Anks

  2. #2
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    did you try
    sort -n
    ???
    New to the internet, technical forums, or the hacker / open source community??
    Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html

    RHCE for RHEL version 5
    RHCT for RHEL version 4

  3. #3
    Just Joined!
    Join Date
    Jan 2009
    Location
    Helsinki, Finland
    Posts
    8
    Yes dear,

    I tried almost everything. -n -g

    But the sort seems to ignore the negative sign before the numbers.

    Please help me out still looking for a solution.

    Regards,
    Anks

  4. #4
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Your sort command works for me. What do you get? Post the output please.

  5. #5
    Just Joined!
    Join Date
    Jan 2009
    Location
    Helsinki, Finland
    Posts
    8
    Quote Originally Posted by vsemaska View Post
    Your sort command works for me. What do you get? Post the output please.
    Well, the list gets sorted but the problem is that its ignoring the minus sign while doing the sort. Do I need to do some kind of setting so that the -ve sign is not ignored. I am on Mandriva and really tired of this trivial problem.

    Thanks,
    Anks

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Again, without seeing the output from the sort of your sample I'm not sure what you're getting. You said you tried the -g option, did you try it like this?

    Code:
    sort -t ' ' -k1,1g -k2,2g file.txt
    This is what I get:

    -0.120218 0.85625
    -0.11 0.848593
    -0.085028 0.833333
    -0.0825 0.832067
    -0.055 0.820524
    -0.0275 0.814485
    -9.21572e-19 0.812056
    0.0275 0.812056
    0.055 0.812056
    0.0825 0.812056
    0.11 0.612056
    0.11 0.812056
    0.1375 0.812056

  7. #7
    Just Joined!
    Join Date
    Jan 2009
    Location
    Helsinki, Finland
    Posts
    8
    Quote Originally Posted by vsemaska View Post
    Again, without seeing the output from the sort of your sample I'm not sure what you're getting. You said you tried the -g option, did you try it like this?

    Code:
    sort -t ' ' -k1,1g -k2,2g file.txt
    This is what I get:

    -0.120218 0.85625
    -0.11 0.848593
    -0.085028 0.833333
    -0.0825 0.832067
    -0.055 0.820524
    -0.0275 0.814485
    -9.21572e-19 0.812056
    0.0275 0.812056
    0.055 0.812056
    0.0825 0.812056
    0.11 0.612056
    0.11 0.812056
    0.1375 0.812056
    Well here is the input file

    -0.120218 0.85625
    0.1375 0.812056
    0.0275 0.812056
    -0.055 0.820524
    0.0275 0.110
    0.055 0.812056
    -0.11 0.848593
    -0.0275 0.814485
    0.0825 0.812056
    -0.0825 0.832067
    -0.085028 0.833333
    0.11 0.812056

    And here is the Output which I am getting with various options suggested and the one tried by me.

    0.0275 0.110
    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 am using exactly the same command as you and others but I am getting the above output very different from what everyone else is getting.

    Thanks for your efforts.
    Anks

  8. #8
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Based on your output it seems that sort on Mandriva Linux isn't converting the fields to numbers but still are treating them as text strings. I'm using Red Hat Linux so I can't help any further. If you Google for 'Mandriva forum' you'll find forums just for that. Maybe you'll have better luck there.

  9. #9
    Just Joined!
    Join Date
    Jan 2009
    Location
    Helsinki, Finland
    Posts
    8
    Ya thats what I too feel.

    Anyway thanks guys thanks for your efforts. I am already too fed up with Mandrake for lots of other reasons.

    Anks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...