Line Removal: How to Remove Lines from Text in 6 Ways

Line Removal: How to Remove Lines from Text in 6 Ways

To remove lines from text, paste the text into a line removal tool and choose what to delete, or use Find and Replace in your editor. The fastest free way is the Line Cleanup tool: paste your text, remove blank or duplicate lines, and copy the result.

Line removal means deleting whole lines you do not need. It does not change the words on the lines you keep. It is the fix for text from a PDF, a chat export, a spreadsheet, or a copied list full of gaps, repeats, and junk lines.

There are four kinds of lines people usually want to remove:

  1. Empty lines. Lines with nothing on them, including lines that only hold spaces or tabs.
  2. Duplicate lines. The same line appearing twice or more.
  3. Lines with a word. Every line that holds a certain word or label.
  4. Line breaks that split a paragraph. A sentence that was cut into short lines and needs to be joined back into one line.

This guide shows 6 ways to do it. Pick the method that matches where your text lives.

Which method should you use?

MethodBest for
Line Cleanup toolQuick browser cleanup, no install
Notepad++Windows text files
VS CodeCode and logs
Microsoft WordWord documents
Google Sheets and ExcelSpreadsheet lists
grepMany files, same cleanup

1. Remove lines with the Line Cleanup tool (no install)

This is the fastest way when you just want clean text and you do not want to install anything. The tool works in the browser and is free.

  1. Open the Line Cleanup tool.
  2. Paste or type your text into the box.
  3. Pick the cleanup you need: remove blank or empty lines, remove duplicate lines, trim spaces from lines, or remove or replace unwanted line breaks.
  4. Check the result. A line that looks empty may hold spaces or tabs, so trim the spaces first, then remove the empty lines.
  5. Copy the cleaned text and paste it where you need it.

The tool is built for copied text, keyword lists, and URL lists. When you join lines, it replaces each break with a space, so the last word of one line does not stick to the first word of the next.

2. Remove lines in Notepad++

Notepad++ is a free text editor for Windows. Its Edit menu has built-in line removal commands, so you do not need a plugin.

Remove empty lines:

  1. Open your file in Notepad++.
  2. Go to Edit, then Line Operations.
  3. Click “Remove Empty Lines”. This removes every line that holds no characters.
  4. If your file has lines that hold only spaces or tabs, use “Remove Empty Lines (Containing Blank characters)” instead.

Remove duplicate lines:

  1. Go to Edit, then Line Operations.
  2. Click “Remove Duplicate Lines”. It keeps only the first copy of each full line.
  3. Use “Remove Consecutive Duplicate Lines” when you only want to remove repeats that sit right next to each other.

Join lines into one paragraph:

  1. Select the lines you want to join.
  2. Go to Edit, then Line Operations, and click “Join Lines”. It joins the selected lines by replacing each line ending with a single space.

The same Edit menu also has Blank Operations, with Trim Trailing Space and Trim Leading Space. Trim first when a “blank” line is really a line full of spaces.

3. Remove lines in VS Code

VS Code is a free editor from Microsoft. It has two useful ways to remove lines: a shortcut for one line at a time, and Find and Replace with regular expressions for the whole file.

Delete one line:

  1. Put your cursor on the line.
  2. Press Ctrl+Shift+K on Windows or Linux, or Cmd+Shift+K on Mac. The line is deleted at once.

Remove every empty line:

  1. Press Ctrl+H on Windows or Linux, or Cmd+Option+F on Mac, to open Find and Replace.
  2. Turn on the regular expression option. It is the .* button in the Find box, or press Alt+R on Windows or Linux.
  3. In the Find box, type ^\s*\n.
  4. Leave the Replace box empty.
  5. Click Replace All. Every empty line, including lines that hold only spaces or tabs, is removed.

Remove every line that holds a word:

  1. Open Find and Replace with Ctrl+H.
  2. Turn on the regular expression option.
  3. In the Find box, type ^.*word.*\r?\n, and change word to the word you want to remove.
  4. Leave the Replace box empty.
  5. Click Replace All. Every line that holds that word is removed, break and all.

One limit: if the word is on the last line of the file and that line has no line break after it, the pattern may miss it. Delete that line by hand with Ctrl+Shift+K.

4. Remove blank lines in Microsoft Word

Word can remove blank lines with Find and Replace. It uses paragraph marks, written as ^p, to find the gaps.

  1. Press Ctrl+H, or go to the Home tab and click Replace.
  2. In the Find what box, type ^p^p. This finds two paragraph marks in a row, which is a blank line.
  3. In the Replace with box, type ^p.
  4. Click Replace All.
  5. Click Replace All again, and keep clicking until Word reports 0 replacements. Each pass removes one layer of blank lines.

Do not replace a single ^p with nothing. That would join every paragraph in the document into one block of text.

If your blank lines hold spaces, the ^p^p method will miss them. For that case, turn on “Use wildcards” in the Replace dialog, search for ^13{1,}, and replace it with ^p.

5. Remove blank rows in Google Sheets and Excel

When the text lives in a spreadsheet, remove the blank rows there instead of copying the list out and back.

Google Sheets, filter method:

  1. Select the range of cells, then click Data and “Create a filter”.
  2. Click the filter icon on the column that should always have a value.
  3. Under “Filter by values”, uncheck every box except “(Blanks)”, then click OK. Only the blank rows stay visible.
  4. Select those blank rows, right click, and choose “Delete selected rows”.
  5. Click Data and “Remove filter” to show the rest of the data again.

A filter only hides rows. You must delete the visible blank rows in a separate step.

Google Sheets, formula method:

To get a clean copy of the list on a new sheet, use the FILTER function. In an empty cell, type:

=FILTER(A2:B10, NOT(ISBLANK(A2:A10)))

Change the ranges to match your data. The original data stays untouched.

Excel, Go To Special method:

  1. Click one cell in the column that should always have a value on real rows.
  2. Press Ctrl+G to open Go To, then click Special, choose Blanks, and click OK. Excel selects every blank cell in that column.
  3. Right click one of the selected cells, then choose Delete and “Entire row”. Or go to Home, then Delete, then “Delete Sheet Rows”.

Select one column first. If you select the whole sheet, Excel selects every blank cell anywhere, and deleting those rows can delete rows that hold data in another column.

6. Remove lines with grep (Linux and Mac)

grep is a command line tool that is already installed on Linux and Mac. It reads a file line by line and prints only the lines that match, or with -v, only the lines that do not match. Use it when you need to clean many files the same way.

Remove every empty line:

grep -v '^$' file.txt > clean.txt

Remove every line that holds a word:

grep -v 'word' file.txt > clean.txt

Change word to the word you want to remove, and file.txt and clean.txt to your file names.

Remove lines that hold only spaces or tabs:

grep -v '^[[:space:]]*$' file.txt > clean.txt

The pattern ^$ matches a line with nothing between its start and its end, and -v inverts the match, so grep prints every line except the matches. grep does not change the original file. The cleaned text goes into the new file named after the > sign.

What line removal does not fix

Line removal deletes whole lines. It does not fix three nearby problems.

Mixed line endings. A file can mix Windows line endings (CR LF) and Unix line endings (LF). Removing blank lines does not fix that mix. To learn what CR, LF, and CRLF mean and how to convert them, read What Is a CR Line Ending? CR, LF, and CRLF Explained.

Extra spaces inside a line. Line removal only deletes whole lines. It does not remove double spaces between words. For that, use the trim options in the Line Cleanup tool or read How to Remove Extra Spaces in Excel and Google Sheets.

Joined words. When you join lines into one paragraph, always put a space where the break was. If you delete the break and add nothing, the last word of one line joins the first word of the next.

Pick the right method

For a quick paste, use the Line Cleanup tool. For a Windows text file, use Notepad++ Line Operations. For code and logs, use VS Code Find and Replace. For a Word document, use ^p^p to ^p and repeat until Word reports 0 replacements. For a spreadsheet, filter the blank rows in Sheets or use Go To Special in Excel. For many files at once, use grep with -v.

Frequently asked questions

How do I remove all blank lines from text?

Paste the text into the Line Cleanup tool and choose to remove blank lines, then copy the result. In Notepad++, use Edit, then Line Operations, then “Remove Empty Lines”. In Word, use Find and Replace with ^p^p in the Find box and ^p in the Replace box, and click Replace All until Word reports 0 replacements.

How do I remove every line that contains a word?

In VS Code, open Find and Replace with Ctrl+H, turn on the regular expression option, search for ^.*word.*\r?\n with the word you want to remove, and Replace All with an empty Replace box. On the command line, run grep -v 'word' file.txt > clean.txt.

How do I remove duplicate lines?

Paste the text into the Line Cleanup tool and choose to remove duplicate lines. In Notepad++, use Edit, then Line Operations, then “Remove Duplicate Lines”. It keeps only the first copy of each line.

Can I remove lines in Word without installing anything?

Yes. Press Ctrl+H, type ^p^p in the Find what box, type ^p in the Replace with box, and click Replace All. Repeat until Word reports 0 replacements. This works in Word for Microsoft 365, Word for Mac, and Word for the web.

What is the difference between removing lines and removing line breaks?

Removing lines deletes whole lines, including empty lines and duplicate lines. Removing line breaks joins lines together into one line. If a paragraph was split into short lines and you want it to read as one line, remove the line breaks, not the lines. The Line Cleanup tool does both.

References

  1. Quick Text Repeater. Line Cleanup Tool. https://quicktextrepeater.com/line-cleanup-tool/
  2. Notepad++ User Manual. Editing. https://npp-user-manual.org/docs/editing/
  3. Visual Studio Code Docs. Basic Editing. https://code.visualstudio.com/docs/editor/codebasics
  4. Visual Studio Code Docs. Default Keyboard Shortcuts Reference. https://code.visualstudio.com/docs/reference/default-keybindings
  5. Microsoft Support. Find and replace. https://support.microsoft.com/en-us/office/find-and-replace-e631a08c-3456-4d73-9472-c83385231468
  6. Microsoft Support. Replace Text. https://support.microsoft.com/en-us/word/replace-text
  7. Microsoft Learn Q and A. Deleting blank lines in Word. https://learn.microsoft.com/en-us/answers/questions/5019800/deleting-blank-lines-in-word
  8. Microsoft Learn Q and A. Excel data clean up, remove blank rows. https://learn.microsoft.com/en-us/answers/questions/5278992/excel-data-clean-up-remove-blank-rows
  9. Google Docs Editors Help. Sort and filter your data. https://support.google.com/docs/answer/3540681
  10. Google Docs Editors Help. FILTER function. https://support.google.com/docs/answer/3093197
  11. GNU Grep 3.12 Manual. https://www.gnu.org/software/grep/manual/grep.html