Bash

Unique While Preserving Order

admin by @admin ADMIN
1h ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
`sort -u` re-orders. `awk '!seen[$0]++'` is the classic order-preserving dedupe — and it's a single line.
Bash
Raw
# Classic — keep first occurrence of each line, preserve original order
awk '!seen[$0]++' messy.txt

# Dedupe by a specific column (e.g. first column)
awk '!seen[$1]++' transactions.csv

# Dedupe a comma-separated list in one variable
echo "apple,banana,apple,cherry,banana,date" | \
    tr ',' '\n' | awk '!seen[$0]++' | paste -sd ','
# apple,banana,cherry,date

# Dedupe ignoring case
awk 'BEGIN{IGNORECASE=1} !seen[tolower($0)]++' file.txt
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.