Useful Tools: Notepad++ and regex to the rescue

Notepad++ is one of my favorite text editors and I tend to install it on any computer I’m working with. While the built-in Windows Notepad only gets you so far, the free GPL Licensed Notepad++ has some big advantages:

1. Can handle the large text files (think 100MB log files) with ease.

2. There is a great Compare plugin in the Plugin Manager that lets you easily compare two text files.  I use this all the time when comparing firewall or switch configurations to see what has changed (or what needs to change when doing firewall migrations to new hardware.

3. You can open up multiple text files in tabs in Notepad++ and search across all the documents at the same time.

4. It is language specific.  Open up an HTML file in Notepad++ and it knows about HTML formatting and changes the text color appropriately.  Same thing with other formats like XML, PowerShell, VBScript, etc.

Yesterday  I was asked by a colleague to parse a Cisco switch configuration to find ports that are configured differently froman example port on a switch with several hundred ports on it.  The example text would look something like the following:

interface GigabitEthernet2/0/27
switchport access vlan 232
spanning-tree portfast

interface GigabitEthernet2/0/28
description I’m different
switchport access vlan 232
spanning-tree portfast

interface GigabitEthernet2/0/29
switchport access vlan 232
spanning-tree portfast

Ideally, we’d just flag port 2/0/28 as different in the example above, since it doesn’t match the other two ports.

While a regular Find/Replace operation would find the particular lines to be deleted, it wouldn’t be able to handle the changes in the port numbers.  That’s where the regular expression (regex) feature of Notepad++ can help.  Regex is a very powerful method for handling text files but can be very confusing.

There’s a great regex testing tool here: https://regex101.com/ so you can experiment with building your own regex expressions.

Let’s go through this example:

Our main problem is that we need to search through all the ports and ignore the port numbers in the example above, so that we can find all the ports regardless of number: “interface GigabitEthernet2/0/27”.  To do this we need to search for the following:

interface GigabitEthernet\d*\/\d*\/\d*

Where the search terms are the following:

\d = any single numeric digit
* = repeat the previous token (the \d in this case from 0 to infinite times).
\/ = This is the escape character “\” followed by the forward slash character “/” which searches for a forward slash “/”

By combining those together we have a string that will search for all the ports in the configuration.  The only remaining issue now is the carriage returns in the file.  These can be found using \r for carriage return and \n for new line.  So the full search string we are looking for is now:

interface GigabitEthernet\d*\/\d*\/\d*\r\n switchport access vlan 232\r\n spanning-tree portfast\r\n

Now just do a replace in Notepad++ (make sure to choose the regular expression radio box) and replace that text with nothing so that the matching ports are removed from the text configuration.

Notepad++ Regular Expression Search showing Regular Expression Radio Box Checked
Notepad++ Regular Expression Search

Congratulations, now the only ports that should be left in the configuration are ports that are “different” and would need to be handled separately.

Do you have a favorite feature of Notepad++?  Do you have some favorite regex expressions that you use all the time?  Do you have some other favorite, must have tool that you’d like to share?  Please let me know in the comments.

Note: This article has also been published on the Kraft & Kennedy website

My Favorite Baklava Recipe

Since people ask for my baklava recipe I thought I would post it here to make it easier to share. Remember I’ll need to have a sample to confirm that you have made it correctly. Several pieces actually would be nice.

This version uses honey rather than a sugar syrup, so differs from many of the other baklava recipes that I’ve tried.  Both are good, but I just tend to like this version.

Tools:

8″ x 12″ baking pan
Pastry brush (or I guess you could use a good paint brush (without paint))

Ingredients (most amounts are approximate):

2 lb (2 boxes) Filo dough (in the freezer or dairy section of your supermarket)
1/2 lb butter (226 g)
3 cups chopped walnuts
2 tsp cinnamon
1/2 tsp ground nutmeg
1/2 tsp ground cloves
1 cup sugar
1 1/2 cup honey (somewhere between 1 and 2 cups should be fine)

Directions:

  1. Preheat oven to 350 F (180 C)
  2. Defrost the filo dough according to directions. If you don’t do this and are working with frozen filo dough, you will not be happy.
  3. Melt the butter in a bowl in the microwave. Melt additional butter as required.
  4. Brush the bottom of the pan with the butter.
  5. Lay down a single sheet of filo dough on the bottom of the pan and brush the top with melted butter.
  6. Continue laying down more filo sheets (buttering the tops every layer) until you have about 6-8 layers. Don’t worry if the filo breaks into pieces smaller than full sheets. Just patch a layer together from small sheets.
  7. Combine the chopped nuts, cinnamon, cloves, sugar, and nutmeg in a bowl. Spread about 1/4 of the mixture over the filo dough. Make sure you have spread the mixture evenly (don’t forget the edges and corners)
  8. Lay down another 4-6 sheets of filo (buttering each layer).
  9. Lay down 1/4 of the chopped nuts mixture.
  10. Lay down another 4-6 sheets of filo (buttering each layer).
    Lay down 1/4 of the chopped nuts mixture.
  11. Lay down another 4-6 sheets of filo (buttering each layer).
    Lay down remaining 1/4 of the chopped nuts mixture.
  12. Lay down remaining filo sheets (do the math ahead of time if it makes you happy, you need about 5 layers, with the top and bottom layer thicker than the in between layers). Butter between sheets.
  13. Butter the top of the filo with remaining butter.
  14. Cut the filo (before baking) into whatever shapes you want (typically diamond or triangle shapes). Make sure you cut all the way through to the bottom.
  15. Bake for approximately 45 minutes to 1 hour at 350F (180C).
  16. Immediately after removing baklava from oven pour honey over the top while hot. Make sure to cover baklava completely with honey.
  17. Let cool and serve.