Deduplicate

3 snippets across 3 stacks - Bash & Linux, Python, SQL

SHBash & Linux

Filter Duplicate Lines

SH · File Content
Syntax
uniq [options] [input [output]]
Example
sort access.log | uniq -c | sort -rn | head -20
Output
    847 GET /api/users
    623 GET /api/health
    412 POST /api/login

Note uniq only removes adjacent duplicates, so you almost always need to sort first. -c prefixes each line with its count, -d shows only duplicates, -i ignores case.

PYPython

SQLSQL

DISTINCT

SQL · Basic Queries
Syntax
SELECT DISTINCT column FROM table;
Example
SELECT DISTINCT city
FROM users
ORDER BY city;
Output
-- Returns each city only once, no duplicates

Note DISTINCT applies to the entire row when used with multiple columns. SELECT DISTINCT city, state treats (city, state) pairs as the unit of uniqueness.

Frequently asked questions

How does Bash & Linux handle deduplicate?
This task is covered in 3 stacks on this page: Bash & Linux, Python, SQL. The "Filter Duplicate Lines" snippet in Bash & Linux uses `uniq [options] [input [output]]`.
Which command does the Bash & Linux example use?
The "Filter Duplicate Lines" snippet uses `uniq [options] [input [output]]`, from the File Content section of the Bash & Linux cheat sheet.
Which stacks cover "deduplicate" on this page?
Bash & Linux, Python, SQL. Together they hold 3 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Filter Duplicate Lines": uniq only removes adjacent duplicates, so you almost always need to sort first. -c prefixes each line with its count, -d shows only duplicates, -i ignores case.