Uniq Command (Linux)

A very interestin gthing about uniq is that it works only on adjacent matching lines. Here’s what the man pages say.

“Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT (or standard output)”

Hence more often than not, it is used in conjunction with sort

$ cat file1.txt
20
19
5
49
5
200

 

$ uniq -c file1.txt
1 20
1 19
1 5
1 49
1 5
1 200
$ sort -n file1.txt | uniq -c
2 5
1 19
1 20
1 49
1 200

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s