uniqは重複した行を表示します。

uniqは重複した行を表示します。
$ 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?     

2つの重複行が表示されるのはなぜですか?

出力は次のとおりです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!$

ベストアンサー1

ああ、自由の地で勇敢な人の家です!

上記の行は3回表示されます。最初の2つの項目の終わりにはキャリッジリターンがあります。 3回目にはそうではありません。次のコマンドを使用して可視化できます。

猫 - A american.txt

これは、テキストファイルの末尾に空白行がないために発生します。
私の考えでは、テキストファイルの最後に常に空白行を置くことがUnix / Linuxの世界の習慣だと思います。

テスト中に見つけた他の興味深い点は次のとおりです。

  • このコマンドはcygwinとは異なる動作をします。
  • grep home american.txt | sort| uniq -u素晴らしい作品。

おすすめ記事