Skip to content
Advertisement

Bash sort -nu results in unexpected behaviour

A colleague of mine noticed some odd behaviour with the sort command today, and I was wondering if anyone knows if the output of this command is intentional or not?

Given the file:

JavaScript

We are looking to sort the file with numeric sort, and also make it unique, so we run:

JavaScript

The output is:

JavaScript

Now, we know that the numeric sort won’t work in this case as the lines don’t begin with numbers (and that’s fine, this is just part of a larger script), but I would have expected something more along the lines of:

JavaScript

Does anyone know why this isn’t the case? The sort acts as one would expect if given just the -u or -n options individually.

Advertisement

Answer

You are missing specifying the de-limit on the second field of GNU sort as

JavaScript

The flag -n for numerical sort, -u for unique lines and the key part is to set de-limiter as _ and sort on the second field after _ done by -k2.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement