Reputation: 13
If I run this sort:
printf "Will be second line post-sort\nWill be first line post-sort" \
| sort -k1,1
I get as output:
Will be first line post-sort
Will be second line post-sort
as expected (key fields are the same but the sort uses last-resort comparison). However if I add the -u option:
printf "Will be second line post-sort\nWill be first line post-sort" \
| sort -k1,1 -u
I get as ouput:
Will be second line post-sort
instead of the expected:
Will be first line post-sort
Why does sort -u return first line of the pre-sorted data rather than the first line of the post-sorted data in this case?
BTW I can get the desired behavior by sorting (without the -u) first:
printf "Will be second line post-sort\nWill be first line post-sort" \
| sort -k1,1 | sort -k1,1 -u
Upvotes: 0
Views: 58
Reputation: 780688
I don't think you can depend on precisely which of the lines will be in the output. POSIX says:
-u
Unique: suppress all but one in each set of lines having equal keys.
It doesn't say which of the lines having equal keys will be in the output, just that there will only be one of them. So if you want a specific one, you should use some other tool like awk
Upvotes: 3