雑多なブログ

音楽や語学、プログラム関連の話題について書いています

mac&linux: uniqコマンドの注意点

uniqコマンドは、重複した行をまとめてくれる便利なコマンドです。
ただし、ソートしてからでないとうまく処理されません。

目次

実行環境

macOS Catalina 10.15.4 のterminal

テストデータ(test.txt)

1
7
5
3
2
2
4
8
1
6
7

ソートせずにuniqコマンドで処理

実行するコマンド

uniq test.txt

処理結果

1
7
5
3
2
4
8
1
6
7

ソートしてから、uniqコマンドで処理

実行するコマンド

sort -h test.txt | uniq

結果

1
2
3
4
5
6
7
8