How to delete lines in Notepad++ that contain specified text string

Have you ever had a list similar to this one:

Some text here...spam...some text here
Some text here...good...some text here
Some text here...good...some text here
Some text here...spam...some text here

…and you wanted to delete all the ones that contained a certain string of characters? If so, I have the solution!

  1. Open the file with the text you want to filter in Notepad++. If you don’t have it, google it and download it.
  2. In the top tool bar of Notepad++, click on search/replace to open up the search and replace dialog window.
  3. In the replace dialog window, where it says “find what” enter: .*spam.*
  4. That’s dot star then the text or character string you want to search for followed by another dot star. Replace the word “spam” in my example with the text you want to search for.
  5. Check the box towards the bottom left of the replace dialog window that says “Regular Expression” to enable the use of regular expression search mode.
  6. Leave the “Replace with” field blank so it deletes the lines it finds that text in and doesn’t replace it with something.
  7. Click “Replace All” to the right of the dialog window and it will replace all lines that contain spam with a blank line.

Here’s what your find and replace dialog will look like:

Snap 2014-12-15 at 12.14.49

Delete all blank lines in Notepad++

Now that you have gotten rid of the lines you didn’t want, you may also want to delete all those pesky blank lines with another search & replace trick. Here is how:

Simply go back up to the Notepad++ toolbar and navigate to “Edit/Line Operations/Remove Empty Lines” and it will quickly remove any empty lines without having to do a complicated regex which you can also do if you know how.

 

6 Replies to “How to delete lines in Notepad++ that contain specified text string”

  1. This works in a specific, but to be hyperprecise does not do what the title says.

    In effect the right title should be «How to delete lines in Notepad++ that contain specified text string and blank lines»

    Consider the case where you are dealing with a code file in any language and you want to remove some lines containg for example some temporary variables beginning with “temp”. With this procedure ALSO the blank lines meant to logically separe block of codes are removed.

    Probably it’s possible to do the job correctly marking the lines containing the text then deleting market lines. I gonna try it.

  2. how do I select statements that start with System. I need to delete all the System.out(……); statements.
    I would appreciate the help

Leave a Reply

Your email address will not be published. Required fields are marked *