3

I have a file with semicolon separated fields, which I want to sort according to general numeric value of the 26th column. I tried this:

cat file.txt | grep -v setch | sort -t";" -k26 -g

The grep command is there to filter out some lines I don't want.

The file after the grep command looks like this:

5;0;0;0;0;17;0.040000;3.00;17;0.030000;2.00;17;0.040000;7.00;11.5833330154419;11.5833330154419;11.5833330154419;0.522556364536285;312.500000000000;-1384.20000000000;39.0625000000000;6000.00000000000;;;;;;;;;;33.15;;X;;E;
5;0;0;0;0;17;0.040000;3.00;17;0.020000;3.00;17;0.040000;7.00;11.5833330154419;11.5833330154419;11.5833330154419;0.522556364536285;312.500000000000;-1384.20000000000;39.0625000000000;6000.00000000000;;;;-7.18901342e+02;-7.78309691e+01;-7.78225676e+01;-7.78079745e+01;-7.77838466e+01;;39.3333333333333;;X;;E;
5;0;0;0;0;17;0.040000;3.00;17;0.020000;20.00;17;0.040000;7.00;11.5833330154419;11.5833330154419;11.5833330154419;0.522556364536285;312.500000000000;-1384.20000000000;39.0625000000000;6000.00000000000;;;;-7.78309996e+01;-7.78285783e+01;-7.78259409e+01;-7.78212922e+01;-7.78200550e+01;;39.8166666666667;;X;;E;
5;0;0;0;0;17;0.040000;3.00;17;0.030000;3.00;17;0.040000;7.00;11.5833330154419;11.5833330154419;11.5833330154419;0.522556364536285;312.500000000000;-1384.20000000000;39.0625000000000;6000.00000000000;;;;-9.38492178e+02;-5.44898488e+02;-7.78311132e+01;-7.78228037e+01;-7.78082194e+01;;40.6166666666667;;X;;E;
5;0;0;0;0;17;0.040000;3.00;17;0.030000;8.00;17;0.040000;7.00;11.5833330154419;11.5833330154419;11.5833330154419;0.522556364536285;312.500000000000;-1384.20000000000;39.0625000000000;6000.00000000000;;;;-7.78321216e+01;-7.78265847e+01;-7.78213151e+01;-7.78175760e+01;-7.78102439e+01;;40.4833333333333;;X;;E;
5;0;0;0;0;17;0.040000;3.00;17;0.030000;15.00;17;0.040000;7.00;11.5833330154419;11.5833330154419;11.5833330154419;0.522556364536285;312.500000000000;-1384.20000000000;39.0625000000000;6000.00000000000;;;;-7.78326108e+01;-7.78282041e+01;-7.78246496e+01;-7.78216823e+01;-7.78198536e+01;;40.0333333333333;;X;;E;
5;0;0;0;0;17;0.040000;3.00;17;0.020000;15.00;17;0.040000;7.00;11.5833330154419;11.5833330154419;11.5833330154419;0.522556364536285;312.500000000000;-1384.20000000000;39.0625000000000;6000.00000000000;;;;-7.78317280e+01;-7.78275891e+01;-7.78237230e+01;-7.78209144e+01;-7.78197521e+01;;44.3;;X;;E;
5;0;0;0;0;17;0.040000;3.00;17;0.030000;10.00;17;0.040000;7.00;11.5833330154419;11.5833330154419;11.5833330154419;0.522556364536285;312.500000000000;-1384.20000000000;39.0625000000000;6000.00000000000;;;;-7.78322942e+01;-7.78274590e+01;-7.78225495e+01;-7.78192915e+01;-7.78148301e+01;;43.65;;X;;E;
5;0;0;0;0;17;0.040000;3.00;17;0.020000;8.00;17;0.040000;7.00;11.5833330154419;11.5833330154419;11.5833330154419;0.522556364536285;312.500000000000;-1384.20000000000;39.0625000000000;6000.00000000000;;;;-7.78322863e+01;-7.78266434e+01;-7.78211618e+01;-7.78173451e+01;-7.78097348e+01;;45.4833333333333;;X;;E;
5;0;0;0;0;17;0.040000;3.00;17;0.030000;4.00;17;0.040000;7.00;11.5833330154419;11.5833330154419;11.5833330154419;0.522556364536285;312.500000000000;-1384.20000000000;39.0625000000000;6000.00000000000;;;;-7.61265100e+02;-7.78321802e+01;-7.78247066e+01;-7.78104129e+01;-7.78053976e+01;;44.8833333333333;;X;;E;

The output is however not sorted according to general numerical value, but according to numerical value (without reference to the powers).

Is there anything I can do to get sort to do what I want?

Update: This is the output of the above pipe (only the relevant column shown), and it is also the output of sort -t\; -g -k26,26, which was suggested in the answer.

-9.38492178e+02
-7.78317280e+01
-7.78309996e+01
-7.18901342e+02
-7.78322863e+01
-7.78322942e+01
-7.78326108e+01 
-7.61265100e+02
-7.78321216e+01
5
  • works for me. What version of sort are you using
    – iruvar
    Dec 5, 2015 at 3:48
  • @1_CR I can't check right know, it's on a different machine
    – fifaltra
    Dec 5, 2015 at 4:43
  • what OS & Version is it? Linux? Mac? Solaris? FreeBSD? Other?
    – cas
    Dec 5, 2015 at 5:35
  • Please post an example of the incorrectly sorted output.
    – RobertL
    Dec 5, 2015 at 5:52
  • sort (GNU coreutils) 8.4 is the version, and I'm guessing it's Linux, it's a machine I log in to to do calculations...
    – fifaltra
    Dec 6, 2015 at 5:45

1 Answer 1

3

note the difference in output between these two pipelines:

<yourexample \
sort -t\; -gk26 |
cut -d\; -f26

-7.18901342e+02
-7.78309996e+01
-9.38492178e+02
-7.78321216e+01
-7.78326108e+01
-7.78317280e+01
-7.78322942e+01
-7.78322863e+01
-7.61265100e+02

...and...

<yourexample \
sort -t\; -gk26,26 |
cut -d\; -f26

-9.38492178e+02
-7.61265100e+02
-7.18901342e+02
-7.78326108e+01
-7.78322942e+01
-7.78322863e+01
-7.78321216e+01
-7.78317280e+01
-7.78309996e+01

sorting just on -key 26 is the same as sorting from key 26 through to the end of the line, but sorting on -key 26,26 sorts only on that key. if you want to consider other fields in the sort order as tie breakers, add more -keys - but be specific.


All that aside, you've commented that you're working with a 5-year-old GNU Coreutils package. Curious, I skipped through a few changelogs after your release, and this stood out within two releases (Oct 2010 for v8.6):

sort -g now uses long doubles for greater range and precision.

sort -h no longer rejects numbers with leading or trailing ., and no longer accepts numbers with multiple .. It now considers all zeros to be equal.

You might update.

2
  • Oh, well, I guess I have to ask the administrator of the machine if he can update sort for me, it doesn't look like I can do it myself. Anyway, thanks for the explanation and the hint about the version, at least I now know why it doesn't work.
    – fifaltra
    Dec 7, 2015 at 5:14
  • 1
    @fifaltra - you could locate a personal build in ~/bin or something and PATH=~/bin:$PATH.
    – mikeserv
    Dec 7, 2015 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.