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:

ABC_22
ABC_43
ABC_1
ABC_1
ABC_43
ABC_10
ABC_123

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

sort file.txt -nu

The output is:

ABC_22

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:

ABC_1
ABC_10
ABC_123
ABC_22
ABC_43

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

sort -nu -t'_' -k2 file
ABC_1
ABC_10
ABC_22
ABC_43
ABC_123

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