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

0 Shares

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 tab, 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.

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

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

0 Shares

About the author

Ramesh Srinivasan founded Winhelponline.com back in 2005. He is passionate about Microsoft technologies and has a vast experience in Windows — delivering support for Microsoft's consumer products. He has been a Microsoft MVP (2003-2012) who contributes to various Windows support forums.

Leave a Comment