How to Remove Lines Containing a Word or String in a Text File

If you have hundreds of lines in a text file and want to bulk delete lines that contain a word or string, this article is for you.

Let’s use the excellent third-party text editor Notepad++ (free) for deleting lines containing a word in a text-based file, using different methods.

Remove Lines Containing a Word, Phrase or String in a Text File

Scenario: I have a huge HOSTS file containing thousands of lines in it. I want to remove MSN advertising server entries from the file. In other words, I need to remove lines containing the string “.msn.com” in the HOSTS file. Let’s use Notepad++ for the job.

Related: How to Replace Notepad With Notepad++ or Any Other Text Editor?

Note: As always, be sure to create a backup copy the text file before modifying it.

Method 1: Remove lines using Bookmark feature in Notepad++

  1. Open the text-based file using Notepad++
  2. Press Ctrl + F to open the Find and Replace dialog.
  3. Click to select the Mark tab.
  4. Type the search word or phrase in the “Find what” text box. In this example, I’d be typing .msn.com
  5. Enable the Bookmark line checkbox.
  6. Set Search Mode to Normal.
    remove lines containing a word or string in a text file
  7. Click Mark All.This marks (bookmarks) all the lines containing the string .msn.com, as seen below:
    remove lines containing a word or string in a text file
  8. Close the Find dialog by clicking Close
  9. From the Search menu, click Bookmark, and click Remove Bookmarked Lines.
    remove lines containing a word or string in a text file

This removes all lines that contain the search string/word/phrase in the text file.

How to Remove lines that DO NOT contain a word or string?

To do the opposite of the above — i.e., delete lines that do not contain a word or phrase:

  1. Repeat the steps 1 → 8 above.
  2. In step 9, from the Search menu in Notepad++, click Remove Unmarked Linesremove lines containing a word notepad++

This removes all the lines except the ones which contain the search word or string.



Method 2: Delete lines using Find and Replace method with RegEx

This method uses regular expressions to find and replace lines containing a word or phrase. This method is very powerful as you can match almost anything (such as words “beginning with”, or lines that have a specific “pattern”.)

  1. Open the text-based file using Notepad++.
  2. Press Ctrl + F to open the Find and Replace dialog.
  3. Click the Replace tab to select it.
  4. In the Find what: text box, type the search word, preceded and followed by .*e.g., if you’re wanting to replace lines containing the word books, you’d type .*books.*

    Whereas, it’s slightly different in our case where we have two (special) . (dot) characters in our search string .msn.com. So we need to type the following in the Find what: text box:

    .*\.msn\.com.*
    .* – matches any character any number of repetitions.
    \. – is used to escape the dot (.)So, the system understands you’re looking to match the string .msn.com
  5. Set the Search Mode to Regular expression
  6. Make sure that the Replace with: text box is left blank.remove lines containing a word or string in a text file using regex
  7. Click Replace All.Now, Notepad++ replaces all those matching lines with blank lines. In the Replace dialog, you’ll see the number of occurrences replaced. Next, you need to remove those blank lines.
  8. Close the Find/Replace dialog.
  9. To remove the empty lines, click Edit → Line Operations → Remove Empty Lines.remove lines containing a word or string in a text file

This removes all the lines except the ones which contain the search word or string.

How to Remove lines that DO NOT contain a word or string using Regex?

To do the opposite of the above — i.e., delete lines that do not contain a word or phrase using Regex:

  1. Follow steps 1 to 3 above.
  2. In step 4, use the regex search keyword ^(?!.*\.msn\.com).*$remove lines not containing a word notepad++ regex

    The above search operator finds lines that don’t contain the word or string .msn.com and replaces them with empty lines.

  3. Then follow steps 5 to 9 to remove the empty lines.

That’s it! Hope the above methods proved helpful to quickly remove lines containing (or not containing) a specific string, word or phrase in a text file.


One small request: If you liked this post, please share this?

One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
  • Pin it!
  • Share it to your favorite blog + Facebook, Reddit
  • Tweet it!
So thank you so much for your support, my reader. It won't take more than 10 seconds of your time. The share buttons are right below. :)

About the author

Ramesh Srinivasan founded Winhelponline.com back in 2005. He is passionate about Microsoft technologies and he has been a Microsoft Most Valuable Professional (MVP) for 10 consecutive years from 2003 to 2012.

Leave a Comment