Sorting Scientific Number with Unix Sort

Sorting Scientific Number With Unix Sort

Use -g (long form --general-numeric-sort) instead of -n. The -g option passes the numbers through strtod to obtain their value, and it will recognize this format.

I'm not sure if this is available on all implementations of sort, or just the GNU one.

UNIX sort with exponential values?

sort with '-g' option should do the trick for you.
-g option indicates 'use generic numerical value' for sorting

sort data in scientific notation. sort -g does not work

About you first question :

export LC_NUMERIC=en_US
sort -g data.txt

Test :

0.00000000e+00
1.02272602e-02
1.26666948e-02
1.25536099e-01
1.29036099e-01

About your second question, to sort the second column :

export LC_NUMERIC=en_US
sort -k2.2,2.15g data.txt

Test :

1.00000e+02, 0.00000000e+00
2.00000e+02, 1.02272602e-02
4.00000e+02, 1.26666948e-02
3.00000e+02, 1.25536099e-01
5.00000e+02, 1.29036099e-01

Explanations :

The LC_NUMERIC locale specifies the decimal-point character and thousands separator.

Sorting pos/neg numbers with fractional parts using Unix sort

I am on a Mac, so it may be a slightly different implementation, but I found this to work:

sort -gb -k 4.5,4 inputfile

In English: "sort, in a -general numeric fashion, ignoring -blanks, the file inputfile using the 4th -k(c)olumn's data, from the 5th element in that column to the end of the data in the 4th column"

field1 field2 field3 tag=-1.92 field5 field6
field1 field2 field3 tag=-1.91 field5 field6
field1 field2 field3 tag=0.123 field5 field6
field1 field2 field3 tag=4.22 field5 field6
field1 field2 field3 tag=5.77 field5 field6
field1 field2 field3 tag=INF field5 field6
field1 field2 field3 tag=INF field5 field6
field1 field2 field3 tag=INF field5 field6

How to sort a file in unix both alphabetically and numerically on different fields?

Try using like this:-

sort -k1,1 -k4,4n
  • -n : Makes the program sort according to numerical value
  • -k opts: Sort data / fields using the given column number. For example, the option -k 2 made the program sort using the second

    column of data. The option -k 3,3n -k 4,4n sorts each column. First

    it will sort 3rd column and then 4th column.

Sorting multiple keys with Unix sort

Use the -k option (or --key=POS1[,POS2]). It can appear multiple times and each key can have global options (such as n for numeric sort)

unix sort for 2 fields numeric order

There's a fascinating article on re-engineering the Unix sort ('Theory and Practice in the Construction of a Working Sort Routine', J P Linderman, AT&T Bell Labs Tech Journal, Oct 1984) which is not, unfortunately, available on the internet, AFAICT (I looked a year or so ago and did not find it; I looked again just now, and can find references to it, but not the article itself). Amongst other things, the article demonstrated that for Unix sort, the comparison time far outweighs the cost of moving data (not very surprising when you consider that the comparison has to compare fields determined per row, but moving 'data' is simply a question of switching pointers around). One upshot of that was that they recommend doing what danfuzz suggests; mapping keys to make comparisons easy. They showed that even a simple scripted solution could save time compared with making sort work really hard.

So, you could think in terms of using a character that's unlikely to appear in the data file naturally (such as Control-A) as the key field separator.

sed 's/^\([^.]*\)[.]\([^.]*\)[.]\([^ ]*\) Step \([0-9]*\):.*/\1^A\2^A\3^A\4^A&/' file |
sort -t'^A' -k1,1n -k2,2n -k3,3n -k4,4n |
sed 's/^.*^A//'

The first command is the hard one. It identifies the 4 numeric fields, and outputs them separated by the chosen character (written ^A above, typed as Control-A), and then outputs a copy of the original line. The sort then works on the first four fields numerically, and the final sed commands strips off the front of each line up to and including the last Control-A, giving you the original line back again.



Related Topics



Leave a reply



Submit