Cleaning pasted text: line breaks, spaces, duplicates and sorting
Text copied from a PDF, a Word document, a web page or a spreadsheet rarely arrives clean. Lines break in the middle of sentences, invisible spaces stop a lookup from matching, and lists come with duplicates. Each problem has a simple fix, and doing the fixes in the right order saves a second pass.
By Radhika Singh · Last updated
The order that works
Most cleanups are the same four steps. Do them in this order, because each step makes the next one reliable: duplicates are only found once spacing is consistent, and sorting is only meaningful once each item sits on its own line.
- Fix line breaks, so each paragraph or list item is one line.
- Normalize spaces: collapse runs of spaces, trim line ends, remove invisible characters.
- Remove duplicate lines.
- Sort, if order does not matter.
Line breaks from PDFs and emails
A PDF stores text as positioned lines, not paragraphs, so copying from one puts a hard line break at the end of every printed line. Email clients do something similar when they wrap quoted text. The result looks fine in a narrow window and breaks badly anywhere else.
Remove Line Breaks joins those lines back into sentences. Keep paragraph breaks so blank lines between paragraphs survive, and rejoin hyphenated words so a word split across two lines in the PDF ("conver- sion") comes back as one. To turn a column of values into one line, replace each break with a separator such as a comma.
Line endings themselves differ by system: Windows uses a carriage return plus line feed (CRLF), macOS and Linux use a line feed alone (LF). Most tools accept either, but a file that mixes both can show stray characters or extra blank lines in some editors.
Spaces you cannot see
Two lines that look identical can differ by a trailing space, a tab or a character that takes no width at all. The usual suspects are the non-breaking space (U+00A0), which Word and web pages use to keep words together, and the zero-width space (U+200B), which some sites insert as a line-break hint. A spreadsheet VLOOKUP or a database match fails on them without any visible reason.
Whitespace Remover collapses repeated spaces, trims each line, converts tabs to spaces and removes non-breaking and zero-width characters. Run it before comparing or deduplicating anything you pasted from the web.
Removing duplicates without losing order
Deciding what counts as a duplicate is the real work. Apple and apple are the same email domain but different product names, so choose case sensitivity per job. Ignoring extra spaces catches lines that differ only by spacing, which is why the spacing step comes first.
Remove Duplicate Lines keeps the first occurrence of each line in its original order by default, can show only the removed lines so you can check them, and treats CSV rows specially: it keeps the header and can compare selected columns, so two orders with the same email address count as duplicates even if other columns differ.
Sorting the way people expect
Plain alphabetical sorting compares characters one at a time, so item 10 lands before item 9, and The Beatles files under T. Sort Lines has a natural-number option that sorts item 2, item 9, item 10 in numeric order, and can ignore a leading The, A or An for titles. For CSV data it sorts by a chosen column and keeps the header row on top.
Find and replace, including patterns
When the mess follows a pattern, replace it in one pass. Find and Replace takes several find-and-replace pairs at once, matches whole words or exact case when needed, and supports regular expressions for patterns: \s+$ finds trailing spaces at line ends, and ^\d+\.\s matches list numbering such as "12. " so you can strip it.
Counting characters and words
Limits are counted in different units. Characters, bytes and words all differ once accents and emoji appear: é is one character but two bytes in UTF-8, and many emoji are two UTF-16 code units, which is what JavaScript's length property counts. String Length shows characters, bytes and code units side by side. For content work, Word Frequency Counter shows which words and phrases dominate a text, with common stop words left out.
Tables and Markdown
Cleaned lists often end up in documentation. Markdown Table Generator turns rows pasted from a spreadsheet into a Markdown table, Markdown to HTML renders Markdown for a page or an email, and HTML to Markdown goes the other way, which is the quickest way to strip formatting from text copied out of a web page while keeping headings, links and lists.