I have a text file.

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

How can I do it?

link|improve this question
feedback

migrated from stackoverflow.com May 29 '11 at 16:46

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

6 Answers

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

  1. Search menu "Mark Tab". Activate regular expressions. Search for ^<Path> (^ is for line start). 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.

link|improve this answer
3  
+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
feedback

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++. Its not ideal, but its pretty easy (iff you have excel). Warning: Will not work well with indented lines (excel will shift the columns etc).

link|improve this answer
feedback

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.

link|improve this answer
feedback

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 the first time around. The sed utility uses Regular Expression Basic Syntax (view the reference at http://www.regular-expressions.info/reference.html). 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 \ .

link|improve this answer
feedback

How about using Regular Expressions to replace ^[^,]+ with nothing.

link|improve this answer
Replacing it with "nothing" does not remove the line. – Tim Cooper May 29 '11 at 15:09
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown