Remove Duplicate Lines: Quick Cheat Sheet With 13 Fixes cover image
Prateek Zare

Written by Prateek Zare

Software Developer with ML and Data Expertise, 8+ years of experience

Last updated

Remove Duplicate Lines: Quick Cheat Sheet With 13 Fixes

To remove duplicate lines you only need to decide four things: whether case matters, whether spaces count, which copy survives, and whether order must stay. Get those right and any tool that can remove duplicate lines gives the same answer. Get one wrong and duplicates seem to survive or good lines vanish.

This is a reference page on how to remove duplicate lines. Skim the tables, copy the command you need, and come back when something looks off.

4Settings that change the result
1 passAll a hash based method needs
FirstOccurrence kept by default
OriginalOrder kept unless you sort
Short answerTo remove duplicate lines, paste your list into the Duplicate Line Remover, turn on trim whitespace, leave case sensitive off for ordinary text, keep the first occurrence, and leave sorting off if order matters. On the command line, the same job is awk with a seen array.

The four settings that decide how you remove duplicate lines

Settings that remove duplicate lines and when to flip them
SettingTypical choiceTurn it on whenTurn it off when
Case sensitiveOffIDs, SKUs, code names where NYC and nyc differNames, words, general text
Trim whitespaceOnData came from spreadsheets or formsLeading spaces carry meaning, as in indented code
Ignore empty linesOptionalYou want one block with no gapsBlank lines separate sections you need
Sort resultOffOrder does not matter and you want a tidy listA sequence, ranking or steps must stay in order

When you remove duplicate lines there is a fifth choice that gets less attention: which copy survives. Keep first is right for most lists. Keep last is right for logs and updates, where a later line corrects an earlier one.

Paste your text, set the four options, and get the cleaned list with counts of what was removed. Nothing leaves your browser.

Open the Duplicate Line Remover

One worked example, every option

Take five lines: apple, Banana, apple, cherry, banana. The result depends on the settings, so here is each combination.

Remove duplicate lines: same input, different settings
Case sensitiveKeepLines that surviveCount
OffFirstapple, Banana, cherry3
OffLastapple (line 3), cherry, banana (line 5)3
OnFirstapple, Banana, cherry, banana4
OnLastapple (line 3), Banana, cherry, banana4

Notice that when you remove duplicate lines, turning case on adds a line, because Banana and banana become different values. With case off, keep first retains the capital B and keep last retains the lowercase b. Which spelling you want to keep decides which option to use.

Cheat sheet: deduplicate anywhere

Use the filter buttons to narrow the table to the place where you remove duplicate lines. Every command here was run on the five line sample above.

Commands and formulas to remove duplicate lines by environment
TaskHowKeeps order?
Remove duplicates, first copy winsPaste text, press Remove DuplicatesYes
Ignore capitalisationLeave Case sensitive offYes
See what was removedRead the duplicate list with countsNot applicable
Remove duplicates, keep firstawk '!seen[$0]++' file.txtYes
Same, ignoring caseawk '!seen[tolower($0)]++' file.txtYes
Keep the last copytac file.txt | awk '!seen[$0]++' | tacYes
Sorted unique listsort -u file.txtNo, sorted
Show only repeated linessort file.txt | uniq -dNo, sorted
Count each linesort file.txt | uniq -cNo, sorted
Excel or Google Sheets, new list=UNIQUE(A1:A100)Yes
Excel, in placeData tab, Remove DuplicatesYes
Python, keep firstlist(dict.fromkeys(lines))Yes
JavaScript, keep first[...new Set(lines)]Yes
uniq alone cannot remove duplicate lines

The uniq command only collapses lines that sit next to each other. An unsorted file with apple on line 1 and line 3 keeps both. Sort first, or use the awk form, which tracks every line it has seen.

Why spaces make duplicates survive

Two lines that look identical can differ by one invisible character. The usual culprits are a trailing space, a tab, and a Windows line ending. The tool splits on both Windows and Unix line endings, but trimming is what removes stray spaces.

Lookalike lines when you remove duplicate lines
Line ALine BSame with trim off?Same with trim on?
appleapple followed by a spaceNoYes
appleAppleOnly with case offOnly with case off
appleapple followed by a tabNoYes
appleapple with a non breaking spaceNoCheck the result

The last row is a known trap when you remove duplicate lines. A non breaking space copied from a web page or a document is a different character from a normal space, and some trim routines leave it alone. If a duplicate survives with trim on, search the line for odd spacing.

Quick shortcuts

  • Trim first, then compare. Most surviving duplicates are a trailing space.
  • Case off for people and words, case on for codes.
  • Keep first for lists, keep last for logs.
  • Sort only if order is not meaningful. Sorting uses locale aware order, so it can differ from a raw character sort.
  • Check the count pills. Original lines minus unique lines should equal lines removed.
  • Keep the original file. Dedupe a copy so you can undo.

Why a hash based method is so fast

The simple idea is to compare every line with every other line. That is slow. A faster idea is to remember what you have seen in a lookup table, so each new line needs one check. JavaScript objects and sets both give near instant lookups. MDN notes that a Set holds each value only once and iterates in insertion order, which is why the set based one liners keep your original order.

Comparisons needed to remove duplicate lines by method
LinesCompare every pairLookup table
1,000499,5001,000 lookups
10,00049,995,00010,000 lookups
100,0004,999,950,000100,000 lookups

The pair count to remove duplicate lines by brute force is n times n minus 1, divided by 2. That growth is why a browser tool handles thousands of lines instantly and a naive script can hang.

When the duplicate is only part of a line

A tool that removes duplicate lines compares the whole line. That is what you want for a list of words or emails. It is not what you want for a CSV file where two rows share an ID but differ in a date column. For that, compare one field only.

Remove duplicate lines by column one of a CSV, keeping the first row

awk -F, '!seen[$1]++' contacts.csv

The same way to remove duplicate lines scales to any key. Pick the part of the line that defines identity, store it in the lookup table, and keep a line only when its key is new. If the data is more complex than comma separated text, convert it first, for example with the CSV to JSON Converter, and dedupe on the field you care about.

Browser tool or command line

Both work. The command line is best when the file is huge, when the job repeats every night, or when it sits inside a script. A browser tool is better when you are on a machine without those commands, when you want to see counts and the removed items at a glance, or when you need to flip case and trim settings and compare results.

Privacy is a fair concern with lists of emails or internal names. This tool runs in your browser and sends nothing out, so it is fine for that kind of list. Still, remove duplicate lines from a copy, keep the source file, and delete the copy when you are done.

Cleanup jobs that need to remove duplicate lines

Typical jobs and the right settings to remove duplicate lines
JobCaseTrimKeepSort
Merge two email listsOffOnFirstOptional
Tidy a keyword list for an ad platformOffOnFirstOn
Collapse a repeated error logOnOnLastOff
Combine tag columns from two CSV filesOffOnFirstOptional
Clean exported URLs before an importOnOnFirstOff

On email lists, case off is the practical choice. In theory the part before the at sign can be case sensitive, but virtually all providers treat it as case insensitive. For merged lists, check the result against a second pass of the Text Diff Tool to see exactly what changed.

Run this before you trust how you remove duplicate lines

Related jobs live nearby. Reorder what is left with the Text Sorter, count what remains with the Word Counter, and read the list of keywords after cleanup with the Keyword Density Checker.

A file has the lines: red, Red, red with a trailing space, RED. With case sensitive off and trim on, how many lines remain?

Trimming turns the third line into red, and with case off all four share the key red. One line remains, and keep first leaves the original red.

Frequently asked questions

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

Use a tool that keeps first occurrences without sorting, such as the Duplicate Line Remover with sorting off. On the command line, awk with a seen array does the same.

What is the difference between keeping the first and the last occurrence?

Keep first retains the earliest copy of each line. Keep last retains the latest, which suits logs and updates where newer lines replace older ones.

Why can I not remove duplicate lines completely?

Usually a hidden difference such as a trailing space, a tab, a different capital letter or an unusual space character. Turn on trim, check the case setting, and look for odd spacing.

Can I remove duplicate lines in Excel?

Yes. Use the Remove Duplicates command on the Data tab, or the UNIQUE function in current versions, to build a de duplicated list from a range.

Does uniq remove all duplicates?

No. It collapses only adjacent repeats. Sort the file first with sort and then use uniq, or use sort with the unique flag.

Is my text sent to a server?

No. The Duplicate Line Remover processes your text in the browser, so lists of emails or internal data stay on your device.

Sources: MDN: Set, a collection of unique values · GNU Coreutils: uniq invocation