← Back to Bash Course | Chapter 11: Process Management | Lesson 9 of 12

uniq

In this page:

  1. uniq

uniq

uniq removes consecutive duplicate lines from sorted input, so it's almost always used after sort in a pipeline. -c prefixes each line with a count of how many times it occurred. Without sorting first, uniq will miss duplicates that aren't adjacent.

Warning: uniq only removes *adjacent* duplicate lines — always sort first if duplicates might not be next to each other.

Example: uniq

bash
#!/bin/bash
printf "apple\nbanana\napple\napple\ncherry\n" | sort | uniq
echo "---"
printf "apple\nbanana\napple\napple\ncherry\n" | sort | uniq -c

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.