Trying to sort two list of numbers and using uniq to get the intersection

sortuniq

I have a file A and B so I used to the following command…

(sort -n A B) | uniq -d

which should give me the numbers which occur in both files.

1
2
2
3
4
5
11
11
12
31

These are the numbers I get from sort -n A B but when I pipe it to uniq -d I only get 11 and not 2. What am I doing wrong?

Best Answer

As the comments indicate, the problem seems likely to be blanks or carriage returns. Either of the following should do the trick:

$ (sort -n A B) | sed -E 's/[^[:alnum:]]+$//' | uniq -d
$ (sort -n A B) | tr -d '\r ' | uniq -d

Some flavors of GNU sed use -r instead to get Extended Regular Expressions. tr is certainly simpler but also more brutal in that it removes the characters whether or not they're trailing.

Related Question