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.
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!
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
Very helpful. Thank you very much indeed!
Beautiful and clean technique! Thanks!!
Wonderful! Thanks
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.
Hi
Please let me know how to run a macro for multiple files
This article was a godsend. Thank you so much!
thank you so much
Brilliant!! Thank you.
Thank You ! Really useful!
This was very helpful to me.
too bad for the way to much adds on the page. I don’t mind an ad but keep it modest.
Thank you very much for the post, this was super helpful. Discovering new Notepad++ tricks 🙂