Windows – uniq showing duplicate lines

newlinestext;uniqwindows

$ grep home american.txt | sort | uniq                                                                                            
A home and a country should leave us no more!                                                                                        
Between their loved home and the war's desolation!                                                                                 
O'er the land of the free and the home of the brave!                                                                                  
O'er the land of the free and the home of the brave!                                                                                  
O'er the land of the free and the home of the brave?     

Why is this showing two duplicate lines?

Here's the output of grep home american.txt | cat -A:

O'er the land of the free and the home of the brave?^M$
O'er the land of the free and the home of the brave!^M$
A home and a country should leave us no more! ^M$
O'er the land of the free and the home of the brave!^M$
Between their loved home and the war's desolation!^M$
O'er the land of the free and the home of the brave!$

Best Answer

O'er the land of the free and the home of the brave!

There are 3 occurrences of the line above. The first two occurrences both have a carriage return at the end. The 3rd occurrence doesn't. you can use the following command to visualize it:

cat -A american.txt

It's caused by the fact that this text file lacks an empty line at the end.
I think it's a convention in the Unix/Linux world to always have an empty line at the end of text files.

Some other things I found interesting while I was testing it:

  • the command works differently in cygwin.
  • grep home american.txt | sort| uniq -u works just fine.
Related Question