Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a text file.

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

How can I do it?

share|improve this question

migrated from May 29 '11 at 16:46

This question came from our site for professional and enthusiast programmers.

10 Answers 10

There is an easy way to achieve this. You need to perform 3 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 - Inverse Bookmark"

    ==> All Line you want to delete are bookmarked.

  3. Go to Menu "Search - Bookmark - Remove Bookmarked lines"

    ==> All Bookmarked lines are deleted.

share|improve this answer
+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 '11 at 11:31
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! – Adam Feb 7 '13 at 14:14
You can skip step 2, because it is (at least since v6.4.5) possible to choose "Remove Unmarked Lines". – Nailuj Sep 27 '13 at 11:00
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. – Danubian Sailor Nov 19 '14 at 10:44
Notepad++ is awesome – Mikey Nov 27 '14 at 13:50

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 ^ (^ 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. Search Menu -> Bookmark -> Remove Unmarked Lines.

    ==> All NON Bookmarked lines are deleted.

share|improve this answer

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.

share|improve this answer

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.).

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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 ( 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 the first time around. The sed utility uses Regular Expression Basic Syntax (view the reference at If 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 \ .

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

I was very hesitant to reply to such an old question and don't have the reputation to add just a comment. It is worth pointing out that in addition to the steps in the accepted answer, Notepad++ allows you to record and save a macro that allows you to automate that process. To do this:

  1. From the Macro menu, click Start Recording
  2. Follow the steps in the accepted answer
  3. Again from the Macro menu, click Stop Recording
  4. Still from the Macro menu, click Save Current Recorded Macro

You will now be able to re-run the steps jsut with a single click.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.