Home › Guides › How to Remove Duplicate Lines from a List
Duplicate lines creep into every list: exports from two systems, merged mailing lists, keyword research, log files. Removing them is easy once you know how each tool decides what counts as a duplicate, and that is also where most mistakes come from. This guide covers the common ways to remove duplicate lines, the differences between them, and the less obvious variations such as keeping only the repeated lines.
Before removing anything, decide how strict matching should be. “Blue widget”, “blue widget” and “Blue Widget ” (with a trailing space) look the same to a person, but a program may treat them as three different lines. The two settings that matter most are:
This made-up keyword list has eight lines, with repeats that differ by case and by a trailing space (the ␠ symbol below marks that space):
blue widget
Red widget
blue widget
red widget␠
green widget
Blue Widget
green widget
yellow widget

| Setting | Output | Lines left |
|---|---|---|
| Remove duplicates, trim spaces, ignore case | blue widget, Red widget, green widget, yellow widget | 4 |
| Remove duplicates, trim spaces, case sensitive | blue widget, Red widget, red widget, green widget, Blue Widget, yellow widget | 6 |
| Keep only the lines that repeat (ignore case) | blue widget, Red widget, green widget | 3 |
| Keep only lines that appear once | yellow widget | 1 |
The same eight lines give four different, all correct, answers depending on what you wanted. That is why the choice of mode matters more than the tool.
If the list is in a text file, an email or a web page, paste it into Remove Duplicates. Choose whether to ignore case, and the cleaned list appears as you type along with counts of how many lines went in, how many came out and how many were removed. Nothing is uploaded. It supports the three modes in the table: remove repeats and keep one copy, keep only lines that repeat, and keep only lines that appear once.
=UNIQUE(A2:A100) returns the distinct values without touching the original. A third argument asks for values that appear exactly once: =UNIQUE(A2:A100, FALSE, TRUE).=UNIQUE(FILTER(A2:A100, COUNTIF(A2:A100, A2:A100)>1))=UNIQUE(A2:A100) returns distinct values, and =UNIQUE(A2:A100, FALSE, TRUE) returns only values that appear once. Matching is case-insensitive.Recent versions have Edit, Line Operations, Remove Duplicate Lines. Older versions only removed consecutive duplicates, so sort the lines first (Edit, Line Operations, Sort Lines Lexicographically Ascending). This matches exact text, so trim trailing spaces first if they could differ.
These all assume a text file with one item per line.
| Goal | Command | Notes |
|---|---|---|
| Unique lines, original order, first copy kept | awk '!seen[$0]++' in.txt | Case-sensitive |
| Same, ignoring case | awk '!seen[tolower($0)]++' in.txt | Keeps the first spelling |
| Unique lines, sorted | sort -u in.txt | Use sort -fu to ignore case |
| Count how often each line appears | sort in.txt | uniq -c | sort -nr | uniq only collapses adjacent duplicates, so sort first |
| PowerShell, first copy kept | Get-Content in.txt | Select-Object -Unique | Case-sensitive |
| Python, order preserved | list(dict.fromkeys(lines)) | Case-sensitive |
To remove duplicates by one column of a CSV rather than the whole line, for example the second column: awk -F, '!seen[$2]++' data.csv. This is simple splitting on commas, so it is only safe if no value contains a comma inside quotes.
There are two reasonable outputs. Keeping the original order preserves meaning when position matters, such as a ranked list or a log. Sorting groups duplicates together and makes the result easier to scan, and it is how sort -u works. If you want both a clean list and a count of repeats, sort first, then count with uniq -c (above), or use sorting by most frequent in the extractor for email lists.
If a list still looks full of duplicates after cleaning, the lines usually differ in a way you cannot see. Check for: trailing and leading spaces, non-breaking spaces copied from web pages, different capitalisation, a trailing full stop, or a different dash or apostrophe character. Trimming, ignoring case and, for tricky text, retyping a few lines are the fixes. True near-duplicates such as “colour” and “color” or a misspelling are not exact duplicates and need a human decision.
Compare the before and after counts. If almost nothing was removed from a list you expected to be full of repeats, matching is too strict. If far too much disappeared, check that you did not select several columns in Excel, where a row is only a duplicate if every selected column matches. For anything important, keep the original and run the cleaning on a copy.
Use a method that keeps the first occurrence, such as the online tool’s “Remove duplicates (keep first)”, Excel’s Remove Duplicates button, or awk '!seen[$0]++' on the command line. Sorting methods such as sort -u change the order.
No. “Apple” and “apple” are treated as duplicates, and the first one is kept.
Use a mode that keeps only the repeated lines, such as the online tool’s “Keep only duplicated items”, or in Excel 365 =UNIQUE(FILTER(A2:A100, COUNTIF(A2:A100, A2:A100)>1)).
They probably differ by a trailing space, capital letters or an invisible character. Trim whitespace and ignore case, then run it again.
To find what two lists share or what is unique to each, use the Compare Lists tool. To merge two lists into one without repeats, paste both into one box and remove duplicates.