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 ...
- 02-03-2009 #1Just 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
- 02-04-2009 #2
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
- 02-04-2009 #3Just 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
- 02-04-2009 #4Linux User
- Join Date
- Jun 2007
- Posts
- 318
Your sort command works for me. What do you get? Post the output please.
- 02-04-2009 #5Just Joined!
- Join Date
- Jan 2009
- Location
- Helsinki, Finland
- Posts
- 8
- 02-04-2009 #6Linux 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?
This is what I get:Code:sort -t ' ' -k1,1g -k2,2g file.txt
-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
- 02-04-2009 #7Just Joined!
- Join Date
- Jan 2009
- Location
- Helsinki, Finland
- Posts
- 8
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
- 02-04-2009 #8Linux 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.
- 02-04-2009 #9Just 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


Reply With Quote
