How to remove duplicate lines from text
Short answer
To remove duplicate lines, keep the first time each line appears and drop any later exact copies. Decide up front whether matching should ignore case and surrounding whitespace, because those choices change what counts as a duplicate.
What counts as a duplicate
Deduplication keeps the first occurrence of each line and removes exact repeats. The catch is defining 'exact'. By default Apple and apple (with a trailing space) are different lines, so the option to trim whitespace and ignore case often matters more than the dedupe itself.
- Case sensitivity: should
Appleandapplebe treated as the same? - Whitespace: should leading and trailing spaces be ignored?
- Order: keep the original order, or sort the result?
- Blank lines: drop them, or keep one?
apple apple
banana -> banana
apple cherry
cherry