Notepad++ Workflow: Find Lines Not Containing30 Apr 2013
For some time now I’ve been using Notepad++ as my primary text editor. Given how much time I spend modifying configuration files, writing scripts and scouring log files its not exactly a surprise that I’ve come up with some nifty workflows for working with text in this editor. Whilst I do occasionally shift back to NoteTab Pro for its immensely useful scripting options I try and stay in Notepad++ as much as I can.
One of the old NoteTab clips I wrote was for removing lines from files where some specified text was not found in the line. I also had variations for lines not starting and not ending with the text. For a while I dropped back into NoteTab to do such file filtering, but after a while I figured out how to do it in Notepad++ and so I thought I’d share the knowledge a little for anyone else who has occasion to do this.
For this exercise I used the keyword combination generator at textMechanic.com to generate a set of combinations of between one and four words from the following list of words: black, blue, brown, green, orange, purple, red, white, yellow.
The basis of this workflow is to bookmark lines based on a search and then to filter based on this. So with my test file loaded into Notepad++ I press CTRL+F to bring up the Find and Replace window. I then select the “Mark” tab.
In this tab I enter my text to search for. In this case I chose the word “blue”. Next, I check the “Bookmark Line” check box and leave the other settings as the default. When I click on the “Mark All” button the search is carried out. In this particular instance I get a pop up window notifying me I have 1529 matches. After clicking OK to dismiss the window each line in the file containing the word “blue” is now marked with a bookmark.
Next I select the Search menu followed by the Bookmark menu. Finally select the Remove Unmarked Lines and this will leave you with only the lines containing your search term - in this case, only lines containing “blue”.
Sometimes however I need to be a bit more specific with what I match. In these cases I can make use of regular expressions. I’m not going to go into detail about this (there are many books and articles that do a much better job than I would if you are interested), but regular expressions allow you to look for patterns relating to your search term. A couple of ones I find myself using are where a search term should be matched at the start or the end of a line and I’ll show you how to modify your search to do this.
In the search window you may have noticed the Search Mode option. Previously I had this set as regular. For matching the my search term at the start or end of a line I select (as you might expect) the Regular Expression option.
Matching for the start of a line is simply a case of prefixing the search term with a caret - “\^”. e.g. “\^blue”
Matching for the end of a line is suffxing the search term with a dollar
- ”$”. e.g. “blue$”
So that’s my quick workflow for basic filtering of a file in NotePad++. More advanced filtering can be achieved with the use of regular expressions and there’s a lot of value in improving your knowledge of regular expressions if you work a lot with text based files (including HTML, XML, logs).