Steam
Steam

Reputation: 9856

Understanding sort command in BASH

I am trying to sort a file called data for learning purposes. Its given in my textbook.

5 27
2 12
3 33
23 2
-5 11
15 6
14 -9

Q1) What is the order of just sort data in this case ?

Q2) I am working in one folder. sort data works, but sort +1n data does not. Why ? I typed it exactly like in the book and I get this error -

sort: cannot read: +1n: No such file or directory

EDIT - The book wants to skip column 1 and sort by column 2. Thats why +n might be used.

I use lubuntu 13 to learn unix bash scripting.

PS - Here is the output of sort data

14 -9
15 6
2 12
23 2
3 33
-5 11
5 27

Upvotes: 2

Views: 2383

Answers (1)

SheetJS
SheetJS

Reputation: 22905

sort by default sorts the entire line lexicographically, so the first sort will be

-5 11
14 -9
15 6
2 12
23 2
3 33
5 27

- comes before 1 (check the ASCII codes for each)

According to the posix standard, the aforementioned sort is correct. GNU SORT (the version used in ubuntu) appears to deviate.

The +1n argument also stems from older versions of sort:

Earlier versions of this standard also allowed the - number and + number options. These options are no longer specified by POSIX.1-2008 but may be present in some implementations.

First, the zero-based counting used by sort is not consistent with other utility conventions.

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html

Putting the facts together, older versions of sort treated -1 as if it were -k 2, so you should use -k2 -n in ubuntu.

Upvotes: 1

Related Questions