What is the difference between the following three sort commands in unix? What is the difference between the following three sort commands in unix? unix unix

What is the difference between the following three sort commands in unix?


The difference is that #1 treats the entire line as a single key, and sorts it lexicographically. The other two have multiple keys, and in particular, while #3 uses the same set of fields as #1, it does so in a very different way. It first sorts the list by the first column (whitespace belongs to the following field, and is significant, unless you specify -b), and if two or more rows have an identical value in the first column, then it uses the second key to sort that subset of rows. If two or more rows are identical in the first two columns, it uses the third key, etc.

In your first case, depending on your locale, you can get different results (try LC_ALL=C sort -k1,4 < file and compare it to, for example, LC_ALL=en_US.utf8 sort -k1,4 < file).

In your second and third case, since the keys are split on transitions from non-whitespace to whitespace. This means the 2nd and following columns have varying sized whitespace prefixes, which affect sorting order, since you don't specify -b.

Also, if you have a mix of spaces and tabs for lining up your columns, that could be messing with things.

I got your same results when I had LC_ALL=en_US.utf8 in my environment, but your expected results (i.e. no differences) using LC_ALL=C (SuSE Enterprise 11.2).