Sort behaviour

I see strange results when sorting with -n options and I wander if somebody can explain it.

Input file and two results:

  
$ cat aa
14
-1
  
11
-1
  
0
-1
  
0
$ sort -u aa
  
-1
0
11
14
$ sort -u -n aa
-1
  
11
14
$ 
 

As you can see, zero disappeared when I used -n option. Why is that?

A line with no number before other (non-blank) text is treated as zero. Combining the -n and -u options causes all but one of the lines with the same numeric value to be deleted. So the empty lines and the lines containing 0 sort together and the -u throws out all but one of those lines. (And, you are left seeing an empty line between the -1 and the 11 .

1 Like