uniq
テキスト処理
書き方
sort [file] | uniq -c隣り合って同じ行だけを一つにまとめます。離れた重複は見えないので、ほぼ必ず sort の後に使います。
よく使うオプション
| オプション | 意味 |
|---|---|
| -c | Prefix each line with how many times it repeated |
| -d | Only the lines that appear more than once |
| -u | Only the lines that appear exactly once |
| -i | Ignore case |
| -f | Skip the first N fields before comparing |
例
sort access.log | uniq -c | sort -rn | headThe most repeated lines.
uniq -d list.txtJust the duplicates.
パイプでつないで使うのが本来の形です。ひとつで全部やるより、三つつないだ方が短くなります。
読み方
- 角括弧 [ ] は省いてよい部分です。
- 三点 … は複数並べられるという意味です。
- オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。
よくある質問
Q. uniq は何をしますか。
隣り合って同じ行だけを一つにまとめます。離れた重複は見えないので、ほぼ必ず sort の後に使います。
Q. どう打ちますか。
sort [file] | uniq -c — 角括弧は省いてよい部分です。
Q. よく使うオプションはいくつですか。
ここにまとめたのは5個です。全部は man uniq にあります。このコマンドはテキスト処理の仲間です。
関連するコマンド
man ページ: man uniq