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.