Tools / Remove duplicate lines
Remove Duplicate Lines
Delete repeated lines while keeping the first occurrence and the original order. Optional case-insensitive matching.
How it works
Duplicate removal is not the same as sorting. Sorting groups equal lines next to each other; this tool keeps your original order and drops later copies, so the first time a value appears is the time it stays. That matters when order carries meaning, such as a priority queue, a changelog or a list of IDs in the order they arrived.
Comparison is exact by default: Apple and apple are different lines, and apple with a trailing space is different again. Those are the two cases that cause most surprises, so both are exposed as options. Turning on case-insensitive matching treats the two capitalisations as one line and keeps whichever came first. Turning on whitespace trimming ignores spaces at the edges of a line, which is usually what you want after pasting from a spreadsheet.
Edge cases
- Order is preserved; the first occurrence wins. This is not the same as
sort | uniq, which reorders. - Case sensitivity and whitespace are separate switches. Enabling both catches the most duplicates but merges lines that differ only in capitalisation.
- Blank lines count as duplicates of each other, so five blank lines become one.
Do it in code instead
The same result with a shell one-liner or a few lines of Python, if you would rather automate it:
Bash (order preserved)
awk '!seen[$0]++' list.txt
Bash (case-insensitive)
awk '!seen[tolower($0)]++' list.txt
Python
seen=set(); out=[]
for l in open('list.txt').read().splitlines():
if l not in seen:
seen.add(l); out.append(l)
print('\n'.join(out))
Excel
Data > Remove Duplicates (removes rows, keeps first)
Questions
- Does it keep the first or the last duplicate?
- The first. Later copies are dropped, so the position of a value in your list never moves. Keeping the last occurrence is a different operation and is not offered here.
- Why are Apple and apple both kept?
- Because matching is case sensitive by default. Untick “case sensitive” to treat them as the same line and keep the first spelling encountered.
- Can it handle a very large list?
- Yes, up to what your browser can hold in memory, which is typically several hundred thousand lines. The comparison uses a set, so performance stays close to linear rather than degrading with size.