言語化の練習

プログラマの日記です。プログラミング・読書感想・雑談が中心です。楽しく働くためにどうしたらよいか日々試行錯誤しています。

【Linux】重複行と件数を出力する

コマンド

$ cat test.txt | sort | uniq -c | grep -v '1 '

処理の流れ

  1. cat test.txt | sort でファイル内容をソートする
  2. uniq -c で重複を除外し件数を出力する
  3. grep -v ‘1 ’ で1件の行を除外する

具体例

$ cat test.txt
AAA
BBB
CCC
BBB
AAA
DDD
AAA
BBB

$ cat test.txt | sort 
AAA
AAA
AAA
BBB
BBB
CCC
DDD

$ cat test.txt | sort | uniq -c 
   3 AAA
   2 BBB
   1 CCC
   1 DDD

$ cat test.txt | sort | uniq -c | grep -v '1 '
   3 AAA
   2 BBB

2017/08/30追記

処理対象のデータリストは空白を含まないデータの前提です。
空白を含むデータに対して実施したい場合は、
grep -v ‘1 '”を調整する必要があります。