I have a text file and want to keep lines started with <Path>, and delete all the other lines.

How can I do it?


11 Answers 11


There is an easy way to achieve this. You need to perform 2 steps.

  1. Go to Search menu > Find... > Select "Mark" Tab. Activate regular expressions. Search for ^<Path> (^ is for line start). Don't forget to check "Bookmark lines" and Press "Mark All"

    ==> All Rows you want to keep got a Bookmark

  2. Go to Menu "Search - Bookmark - Remove unmarked lines"

    ==> All lines that aren't Bookmarked are deleted.

  • 54
    +1. Nice, didn't know about this. There are also options to "Remove unmarked lines" or "Cut/copy marked lines" that can save some time.
    – deizel.
    Oct 10, 2011 at 11:31
  • 6
    I had troubles at first because my version (6.2.3) of Notepad++ doesn't have a menu item named "Mark Tab". Instead, you have to go to "Search" -> "Find..." -> click on the tab "Mark". And you no longer have to inverse the bookmarks. Notepad++ now has "Remove Unmarked Lines". But your tip helped me a lot! Thanks!
    – aakoch
    Feb 7, 2013 at 14:14
  • 23
    You can skip step 2, because it is (at least since v6.4.5) possible to choose "Remove Unmarked Lines".
    – Julian
    Sep 27, 2013 at 11:00
  • 5
    I'm using Notepad++ for so long and I've never known what are 'Bookmarks' before. Amazing how powerful that tool is and how little I know about it. Nov 19, 2014 at 10:44
  • I did not need to add the ^ in my search term, nor use regular expression. Hope this helps someone.
    – sa_leinad
    May 20, 2019 at 5:23

This can actually be done in two steps as of 6.3. I think it can be done earlier than that as I had 5.9 when I first tried it.

Using stema's post as the basis of this answer. There's one less step now. Mark lines and remove unmarked lines. Done. Detailed instructions follow.

  1. Search menu "Find". In the Find dialog, click the "Mark" tab. Activate regular expressions. Search for ^<Path> (^ is for line start). Don't forget to check "Bookmark lines" and Press "Mark All"

    ==> All Rows you want to keep now have a Bookmark

  2. Search Menu -> Bookmark -> Remove Unmarked Lines.

    ==> All NON Bookmarked lines are deleted.

  • 3
    This took me 10 seconds while the other solution took me >20 seconds. Thx!
    – Black
    Jul 26, 2016 at 11:25
  • I did not need to add the ^ in my search term, nor use regular expression. Hope this helps someone.
    – sa_leinad
    May 20, 2019 at 5:23

Clean regex only solution

Two step variant

  1. regex replace


    replace test with your requested text

  2. replace


    with \r\n

Single step variant

Use ^(?!<Path>).*\r\n to replace matches with empty string. Generalized version would be ^(?!.*?test).*\r\n. This won't remove empty line at the end of the file. All other lines are removed, including multiple consecutive empty lines.


  1. (?!) is a negative look up. ^.*test.*$ selects the whole line that contains the requested text.

  2. [\r\n]{2,} matches any \r\n that occurs more then once this is Windows New line. if you have Linux or another operating system you might need to mess with this. the second is to replace it with one return line.

  • 1
    Note to the peoples: Sometimes regex with end of line (EOL) "doesn't work". The \r\n EOL mentioned in the post is what Windows uses and therefore may or may not be what you are looking for. Often on Linux environments it's just \n, or in Mac environments just \r, so if you pull a file from either, it won't be Windows style EOL. However, if you download via FileZilla and ASCII mode triggers, it may change them back into Windows EOL (like \n into \r\n). So if regex is not working, check the EOL style by going to "View > Show Symbols > Show End Of Line". CR = \r. LF = \n.
    – dhaupin
    Dec 13, 2016 at 15:02
  • FINALLY someone who knows Regex. Thank you
    – NoName
    Nov 3, 2021 at 1:57

It seems to me that the easiest way is to just use the "Find All in Current Document" feature and then either copy the results into a new file or select all and replace in the current one.

This would find all lines containing your text and list them at the bottom. Just right click on the search result and copy / paste.

  • You do have to remove the line numbers by replacing \tLine [\d]*: . Still a great answer.
    – Noumenon
    Jan 10, 2017 at 21:21
  • 1
    This won't work for the lines which are so wide that Notepad++ truncates it in the search results window.
    – MasterJoe
    Aug 24, 2019 at 0:37

Better solution with regex replace:


And replace with nothing

  • 3
    Welcome to Super User! Care to explain this for those who might not know what each bit of the regex does? :)
    – bertieb
    May 1, 2018 at 15:47
  • better solution. Jan 24, 2021 at 0:04

Go to menu Search -> Find... -> Activate regular expressions. Search for "^Path" (^ is for line start).

Click on the "Find all in Current Document" button.

The "Find result" window will appear with all the lines the the pattern. Select copy/paste them to a new tab in Notepad++.

In this new tab, got to: menu Search -> Replace... -> Activate regular expressions.

In the "Find what:" field, use the pattern: "Line \d+: ". Leave the "Replace with:" field blank.

Click on the "Replace all" button.


Providing that you actually want to match <Path> and not a file system path, you can try this from a command line using Perl:

perl -pe " if ($_ !~ /<Path>/) { s/$_// } " < in.txt > out.txt

It worked with Strawberry Perl on Windows, so adjust accordingly if the results are not what you expect.


It is clumsy, but copy it all to Excel, and then use =IF(LEFT(A1,6)="<Path>",A1,"") and copy that formula all the way down. Then copy that back to Notepad++. It's not ideal, but it's pretty easy (if you have Excel). Warning: It will not work well with indented lines (Excel will shift the columns, etc.).

  • 1
    When there are a number of ways to handle this task directly, why would you want to destructively copy into another application, process there, and transfer back?
    – Baldrickk
    Feb 15, 2017 at 16:36

There is no easy way to do what you want with Notepad++. You'll need to either download a program to your computer or script something in VB (I assume you're on Windows).

You can do what you want one of two ways with sed. The sed utility is a favorite on *nix and can be found for Windows from the great people at GnuWin (http://gnuwin32.sourceforge.net/packages/sed.htm). You would download this program, and then run your command from the command prompt.

Delete all lines not containing :

sed -i '/^<PATH>/!d' file

Print all lines containing to a new file:

sed -n '/^<PATH>/p' file > newfile

I suggest you use print the lines you want to a new file. The reason for this is that you probably won't get the regex statement for <PATH> the first time around. The sed utility uses Regular Expression Basic Syntax (view the reference at http://www.regular-expressions.info/reference.html). If <PATH> is something like a *nix path (/var/www) then you'll need to escape the / character for your regex to work.

Example: sed -n '/^\/var\/www/p' file > newfile
This will print out all lines that start with /var/www. If I filed to escape the / character, then the command would have thrown an error. You can escape a special character (such as /) with the backslash character \.

  • This may be an old answer but since it actually unbelievably has 2 votes (-1 now from me) wanted to comment on not only how wrong it is, but reiterate in a SINGLE command operation how I just (again) took a >100k line log file down to the 34 lines containing the one word (or phrase) in this case simply "error" in under 3 seconds by simply placing this regex in the FIND WHAT: ^(?!.*?error).*\r\n and leaving REPLACE WITH blank, and do NOT select the ". matches newline" and click "REPLACE ALL". Yes I also love sed and awk but saying NPP cannot do this is just false. Dec 30, 2018 at 18:53
  • all the <PATH> disappeared because you didn't format it as code, and that's an invalid html tag so it's not shown
    – phuclv
    Mar 20, 2022 at 1:50

Use Search->Replace and enter a regular expression like ^[^ ].* and replace all with an empty string using Regular expression. Next step is to find empty lines searching for \n\n replacing with \n using Extended multiple times until 0 occurrences were found. (use \r\n\r\n and \r\n depending on file format). If you have very many empty lines in a row, it is quickier to use \n\n\n\n\n\n\n or even more \n:s in the search string.


As Karsten suggested in a comment, one can use the LineFilter2 plugin, which is straightforward to install and use.

enter image description here

To install:

enter image description here

To launch:

enter image description here

You must log in to answer this question.