Removing Duplicate Lines from Text: The Fastest Ways Across Every Tool

●2 ●94 ●140
calendar_today • schedule2 min read

Duplicate lines show up everywhere — exported CSV files, log outputs, lists copied from spreadsheets, word lists scraped from websites. Here's how to remove them efficiently in every context.

The Browser Approach (Fastest for One-Off Tasks)

If you just need to paste text, remove duplicates, and copy the result — a browser tool is the fastest path. No terminal, no Excel, no script. SnappyTools' duplicate line remover does it client-side with instant results and options for case-sensitivity and blank line handling.

Command Line: sort -u and uniq

The Unix approach:

# Remove duplicates (sorted output)
sort -u input.txt > output.txt

# Remove consecutive duplicates only (preserves order)
# First sort, then uniq
sort input.txt | uniq > output.txt

# Case-insensitive dedup
sort -uf input.txt > output.txt

Key difference: sort -u reorders the output alphabetically. uniq only removes consecutive duplicate lines — so you need to sort first if duplicates are scattered.

To preserve original order while removing duplicates:

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

This one-liner is order-preserving and works on any Unix/macOS system. seen is an associative array; $0 is the full line; !seen[$0]++ is true only the first time each line is encountered.

Python

# Order-preserving dedup
lines = open("input.txt").readlines()
seen = set()
result = []
for line in lines:
    stripped = line.rstrip('\n')
    if stripped not in seen:
        seen.add(stripped)
        result.append(line)

with open("output.txt", "w") as f:
    f.writelines(result)

Or with a list comprehension (Python 3.7+, where dict preserves insertion order):

lines = open("input.txt").readlines()
unique = list(dict.fromkeys(lines))
open("output.txt", "w").writelines(unique)

JavaScript

const text = "line1\nline2\nline1\nline3\nline2";
const lines = text.split('\n');
const unique = [...new Set(lines)];
const result = unique.join('\n');
console.log(result);
// line1
// line2
// line3

Set preserves insertion order in JavaScript, so this is both order-preserving and clean.

Excel / Google Sheets

  1. Select the column with duplicates
  2. Data → Remove Duplicates (Excel) or Data → Data cleanup → Remove duplicates (Google Sheets)
  3. Confirm the column selection and click OK

This removes duplicate rows across the selected columns, not just duplicate cells within a single column.

Case Sensitivity

Most implementations are case-sensitive by default — "Apple" and "apple" are treated as different lines. If you want case-insensitive dedup:

# bash
awk '!seen[tolower($0)]++' input.txt

# Python
seen = set()
result = [line for line in lines if line.lower().rstrip() not in seen and not seen.add(line.lower().rstrip())]

Handling Blank Lines

Removing blank lines alongside duplicates is a common need:

# Remove blanks and duplicates
awk 'NF && !seen[$0]++' input.txt

NF (number of fields) is 0 for blank lines, so the condition skips them.

Summary

  • Browser tool: fastest for one-off tasks, no setup → snappytools.app/remove-duplicate-lines/
  • awk '!seen[$0]++': best command-line one-liner, order-preserving
  • sort -u: fast, but reorders output
  • Python dict.fromkeys(): clean, readable, order-preserving
  • JavaScript Set: idiomatic, order-preserving

Pick the method that fits your environment. They all produce the same result.

Originally published at https://snappytools.app/remove-duplicate-lines/

Want more #webdev & #productivity deep dives?

Join 6,182+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

Free SEO Tools Every Small Business Should Use (2026) | FloxoLab

floxolab - Oct 20

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelski - Apr 23

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelski - Mar 19

Stop Turning Every REST Endpoint into an MCP Tool: A Better Approach to AI Tool Design

zeekwrites - Nov 30

Three Design-to-Code Rules Most Developers Ignore (That Will Make You a Better Engineer)

Joemetry - Sep 26
chevron_left
2.5k Points • 236 Badges
102Posts
0Comments
SnappyTools builds free, fast, browser-based tools for developers, writers, and designers. No signup... Show more

Commenters (This Week)

7 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!