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?
Method 1: Remove lines using Bookmark feature in Notepad++
- Open the text-based file using Notepad++
- Press Ctrl + F to open the Find and Replace dialog.
- Click to select the Mark tab.
- Type the search word or phrase in the “Find what” text box. In this example, I’d be typing
.msn.com
- Enable the Bookmark line checkbox.
- Set Search Mode to Normal.
- Click Mark All.This marks (bookmarks) all the lines containing the string
.msn.com
, as seen below:
- Close the Find dialog by clicking Close
- From the Search menu, click Bookmark, and click Remove Bookmarked Lines.
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:
- Repeat the steps 1 → 8 above.
- In step 9, from the Search menu in Notepad++, click Remove Unmarked Lines
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”.)
- Open the text-based file using Notepad++.
- Press Ctrl + F to open the Find and Replace dialog.
- Click the Replace tab to select it.
- 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 wordbooks
, 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
- Set the Search Mode to Regular expression
- Make sure that the Replace with: text box is left blank.
- 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.
- Close the Find/Replace dialog.
- To remove the empty lines, click Edit → Line Operations → Remove Empty Lines.
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:
- Follow steps 1 to 3 above.
- In step 4, use the regex search keyword
^(?!.*\.msn\.com).*$
The above search operator finds lines that don’t contain the word or string
.msn.com
and replaces them with empty lines. - 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.