Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force linux sort to use lexicographic order

I generated a text file with pseudo-random numbers like this:

-853340442 1130519212 -2070936922 
-707168664 -2076185735 -2135012102 
166464098 1928545126 5768715 
1060168276 -684694617 395859713 
-680897578 -2095893176 1457930442 
299309402 192205833 1878010157 
-678911642 2062673581 -1801057195 
795693402 -631504846 2117889796 
448959250 547707556 -1115929024 
168558507 7468411 1600190097 
-746131117 1557335455 73377787 
-1144524558 2143073647 -2044347857 
1862106004 -193937480 1596949168 
-1193502513 -920620244 -365340967 
-677065994 500654963 1031304603 

Now I try to put it in order using linux sort command:

sort prng >prngsorted

The result is not what I expected:

1060168276 -684694617 395859713 
-1144524558 2143073647 -2044347857 
-1193502513 -920620244 -365340967 
166464098 1928545126 5768715 
168558507 7468411 1600190097 
1862106004 -193937480 1596949168 
299309402 192205833 1878010157 
448959250 547707556 -1115929024 
-677065994 500654963 1031304603 
-678911642 2062673581 -1801057195 
-680897578 -2095893176 1457930442 
-707168664 -2076185735 -2135012102 
-746131117 1557335455 73377787 
795693402 -631504846 2117889796 
-853340442 1130519212 -2070936922 

Obviously, sort tries to parse strings and extract numbers for sorting. And it seems to ignore minus signs.

Is it possible to force sort to be a bit dumber and just compare lines lexicographically? The result should be like this:

-1144524558 2143073647 -2044347857 
-1193502513 -920620244 -365340967 
-677065994 500654963 1031304603 
-678911642 2062673581 -1801057195 
-680897578 -2095893176 1457930442 
-707168664 -2076185735 -2135012102 
-746131117 1557335455 73377787 
-853340442 1130519212 -2070936922 
1060168276 -684694617 395859713 
166464098 1928545126 5768715 
168558507 7468411 1600190097 
1862106004 -193937480 1596949168 
299309402 192205833 1878010157 
448959250 547707556 -1115929024 
795693402 -631504846 2117889796 

Note: I tried -d option but it did not help

Note 2: Probably I should use another utility instead of sort?

like image 932
igor.sol Avatar asked Jul 24 '18 16:07

igor.sol


People also ask

How do I sort a lexicographic order?

Sorting words in lexicographical order mean that we want to arrange them first by the first letter of the word. Then for the words whose first letter is the same, we arrange them within that group by the second letter and so on just like in a language's dictionary(not the data structure).

Does sorted sort Lexicographically?

sorted() function sorts data elements in lexicographical order by replicating the input list and keeping the input list as it is. Strings in Python can be sorted by initially splitting and applying sort.

How do I sort numerical order in Linux?

How to sort by number. To sort by number pass the -n option to sort . This will sort from lowest number to highest number and write the result to standard output. Suppose a file exists with a list of items of clothing that has a number at the start of the line and needs to be sorted numerically.

How do you execute a sort command in Linux?

-k Option: Unix provides the feature of sorting a table on the basis of any column number by using -k option. Use the -k option to sort on a certain column. For example, use “-k 2” to sort on the second column.


1 Answers

The sort command takes account of your locale settings. Many of the locales ignore dashes for collation.

You can get appropriate sorting with

LC_COLLATE=C sort filename
like image 138
borrible Avatar answered Oct 13 '22 09:10

borrible