Tutorial2024-03-20

Remove Duplicate Lines Like a Pro: From Log Files to Data Cleanup

Duplicate lines in log files, CSVs, and configs waste disk space and cause bugs. Here's how to clean them using both command-line tools and online utilities.

#linux#text-processing#deduplication#sysadmin#productivity

I can't tolerate it. Duplicate lines in a file. It's like having two identical books on your shelf — pointless, wasteful, and deeply irritating.

Whether you're cleaning server logs, deduplicating a CSV export, or removing redundant config entries, here's every method I know. From one-liners to GUI tools.

Method 1: The Classic sort -u

sort -u input.txt > output.txt

This sorts the file and removes duplicates. Simple. Clean. One command.

Warning: This changes the order of lines. If order matters, use Method 2.

Method 2: Preserve Order with awk

awk '!seen[$0]++' input.txt > output.txt

This keeps the first occurrence of each line and removes subsequent duplicates. Order preserved.

Method 3: The uniq Command

sort input.txt | uniq > output.txt

uniq only removes adjacent duplicates, so you must sort first. Use -c to count occurrences:

sort input.txt | uniq -c | sort -rn

This shows you which lines appear most often. Great for log analysis.

Method 4: Remove Duplicates from CSV Columns

awk -F',' '!seen[$3]++' data.csv > clean.csv

This deduplicates based on column 3 only. Perfect for removing duplicate entries by email, ID, or any specific field.

Method 5: Online Tool (No Terminal Required)

If you don't want to open a terminal — paste your text into a Remove Duplicate Lines tool. It handles the deduplication instantly in your browser.

Real-World Use Cases

Cleaning Server Logs

# Remove duplicate error messages from today's log
grep "ERROR" /var/log/app.log | sort -u | wc -l
# Output: 47 unique errors (from 2,341 total ERROR lines)

Deduplicating Email Lists

# Remove duplicate emails (case-insensitive)
awk '!seen[tolower($0)]++' emails.txt > clean_emails.txt

Cleaning Kubernetes Config

# Find duplicate resource definitions
grep "kind:" deployment.yaml | sort | uniq -d

Performance Comparison

Method 1M Lines Preserves Order Case-Insensitive
sort -u 0.8s
awk 1.2s Optional
sort | uniq 1.1s Optional
Online tool ~3s Optional

For files under 10MB, any method works fine. For larger files, sort -u is fastest.

Common Pitfalls

  1. Trailing whitespace"hello ""hello". Trim first: sed 's/[[:space:]]*$//'
  2. Line endings — Windows CRLF vs Unix LF causes "invisible" duplicates: dos2unix input.txt
  3. Encoding — UTF-8 BOM at the start of a file creates a phantom duplicate: sed '1s/^\xEF\xBB\xBF//'

Clean your text files instantly with our free Remove Duplicate Lines tool — paste, click, done. No terminal required.

🛠

Try It Yourself

Put what you've learned into practice with our free online tools.