If I sort a file, I want save it in the new one. I use for this just:

sort -nk{fieldnumber} test.json > testsort.json

In the output the columns are shuffled and the column, which I have sorted, is in random order (not ascending). Why is it happen? Also, I want split this sorted file in many parts. I wish all parts are also sorted by the column, like here:

{'a':'a', 'b':'b', 'c':1}
{'a':'a', 'b':'b', 'c':1}
{'a':'a', 'b':'b', 'c':2}

{'a':'a', 'b':'b', 'c':2}
{'a':'a', 'b':'b', 'c':3}
{'a':'a', 'b':'b', 'c':3}

{'a':'a', 'b':'b', 'c':4}
{'a':'a', 'b':'b', 'c':5}
{'a':'a', 'b':'b', 'c':6}

I use for it

split -l 100 testsort.json part

Anyway, I have the all time the shuffled, unordered output. (for example some value from partac could be lesser then from partab)


Thank you, I have found the problem someself. The input was corrupt, because some lines have additional fields.

{'a':'a', 'b':'b', 'c':2}
{'a':'a', 'b':'b', 'c':1}
{'a':'a', 'b':'b', 'c':2}
{'a':'a', 'b':'b', 'd':'d', 'c':1}
{'a':'a', 'b':'b', 'c':3}
{'a':'a', 'b':'b', 'f':'f', 'c':4}

I construct next regular expression, which search for certain field, take the numeric values, bring them in the front and sort them

sed -e 's/^\(.*columnname\)\([^,]*\)/\2\t\1\2/;s/^[" :]*//' test.json | sort -nk1 |sed -e 's/^[^{]*//' > testSort.json
  • You say the command worked? Now I have a question ;) See my answer. Sep 19, 2014 at 8:30
  • If the solution, and how you found it, can be of use for other readers, write an answer please! (If it does not help to understand the question really, let's just wait for it to get closed) Sep 19, 2014 at 8:38
  • Ah, I see, the extra columns like 'f':'f', makes itcomplicated with sort alone; It is not made for this kind of data. So sed is a good idea. Sep 19, 2014 at 9:24

1 Answer 1


The field separator of sort is not specified in your command:

sort -nk25

By default, the field separator of sort consists of whitespace1.
That means you sort on the 25th field of 3 fields. It's not really clear on what you want to sort - but you could try to sort on whitespace separated column 3 with:

sort -nk3

Depending on what exactly you see as columns, you may run into the problem that the field separator can only be a single character, not a set of characters, (or even a regular expression) - except for the special case described below.
So you can use the quote as field separator, like sort -t"'" ..., and sort on the character values; It does not work like this with the numbers, because they are not quoted.

(1): The default field separator does not consist off a single or fixed count of space characters. The separator is a non-blank to blank transition - that makes any string of whitespace that does not start in the first column a separator.

  • thank you, but the main problem is, a file is corrupt.... Some lines have additional fields and it doesn't make a sense sort by some field (column), because all values are shifted.. I already construct some solution with regular expression.
    – Guforu
    Sep 19, 2014 at 8:36
  • Good! If you can add that shifted input to the question, I'd be curious whether it can be solved by sort to; The sort key definitions of sort are so powerfull that the description is outright scary ;) Sep 19, 2014 at 8:42
  • I'm very sorry, it was unclear. -nk25 came from the real programm, of course not correlated with the toy example here. Sorry, I have supposed, it would be clear...
    – Guforu
    Sep 19, 2014 at 8:46
  • Looks like sort -nk3 does not work either, because numeric sort does not ignore the non-digits. Sep 19, 2014 at 9:01
  • I suppose -nk9 will sort the table without additional fields.
    – Guforu
    Sep 19, 2014 at 9:03

Not the answer you're looking for? Browse other questions tagged .