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. It won't take more than 10 seconds of your time. The share buttons are right below. :)

Ramesh Srinivasan is passionate about Microsoft technologies and he has been a consecutive ten-time recipient of the Microsoft Most Valuable Professional award in the Windows Shell/Desktop Experience category, from 2003 to 2012. He loves to troubleshoot and write about Windows. Ramesh founded Winhelponline.com in 2005.

12 thoughts on “How to Remove Lines Containing a Word or String in a Text File”

  1. In Method 2: Delete lines using Find and Replace method with RegEx,
    you can add \r\n add the end of RegEx to select whole line with a new line character. It will remove line without leaving an empty one

    Reply
  2. I can confirm that putting \r\n into the find box after the second .* at the end of the word selects the line and new line marker. I can also confirm that you can do this, and then add another .*word.*\r\n to search the next line. If, like me, you are editing some XML files from premiere pro or similar to get the versioning to match and drop a few non-compatible pieces for an older version, you can search whole xml blocks with only the main descriptors and none of the actual values needed. Wipes them all out at once. I got a 2020 file to open up in 2013, and then from there saved again, did this again and got it to open in CS6. There was a lot of code to go through, and thankfully I’m OCD enough to mark all of my edit decisions in basic markers, which allowed me to redo whatever I needed to in CS6, then move straight into Encore for a DVD.
    There are other applications of this as well; I can think of parsing data for xml based databases that have incompatible strings or blocks, or even some old flash-based games that use XML for the adventurer file instead of Binary hexadecimal strings.

    Reply

Leave a Comment