ListFerret mascot

Home › Guides › How to Remove Duplicate Lines from a List

How to Remove Duplicate Lines from a List: Excel, Google Sheets, Notepad++ and the Command Line

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.

What counts as a duplicate?

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:

A worked example

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
The Remove Duplicate Lines tool turning eight lines into four with case-insensitive matching: blue widget, Red widget, green widget, yellow widget
With trimming and “ignore case” on, 8 lines become 4. The sample data is invented for illustration.
SettingOutputLines left
Remove duplicates, trim spaces, ignore caseblue widget, Red widget, green widget, yellow widget4
Remove duplicates, trim spaces, case sensitiveblue widget, Red widget, red widget, green widget, Blue Widget, yellow widget6
Keep only the lines that repeat (ignore case)blue widget, Red widget, green widget3
Keep only lines that appear onceyellow widget1

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.

Remove duplicate lines online

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.

Excel

Google Sheets

Notepad++

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.

The command line

These all assume a text file with one item per line.

GoalCommandNotes
Unique lines, original order, first copy keptawk '!seen[$0]++' in.txtCase-sensitive
Same, ignoring caseawk '!seen[tolower($0)]++' in.txtKeeps the first spelling
Unique lines, sortedsort -u in.txtUse sort -fu to ignore case
Count how often each line appearssort in.txt | uniq -c | sort -nruniq only collapses adjacent duplicates, so sort first
PowerShell, first copy keptGet-Content in.txt | Select-Object -UniqueCase-sensitive
Python, order preservedlist(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.

Keep the first occurrence, or sort?

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.

Near-duplicates that survive

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.

Check your result

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.

Frequently asked questions

How do I remove duplicate lines but keep the original order?

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.

Is Excel’s Remove Duplicates case-sensitive?

No. “Apple” and “apple” are treated as duplicates, and the first one is kept.

How do I find only the duplicates?

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)).

Why are my duplicate lines not being removed?

They probably differ by a trailing space, capital letters or an invisible character. Trim whitespace and ignore case, then run it again.

Can I remove duplicates across two lists?

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.

Try it now: use the free Remove Duplicates - private, instant and runs in your browser. Open Remove Duplicates →

More guides

Share this page: XFacebookLinkedInRedditEmail