Wednesday, May 22, 2013

Batching Find/Replace Using Notepad++

I did this a while ago when I was preparing a lot of config files (.txt) for use on my routers & switches. I needed to find interface numbers and replace them with interfaces that I had available or was using (i.e. s0/0/0 with s0/0, etc.). As I don’t use this feature very often, it is a prime candidate for a blog post.

In Example 1 I will show how I converted s0/0/0 in a folder of text files (approx. 33 files) to s0/0 quickly using Notepad++. In Example 2 I will demonstrate my current need to take a few files and insert a carriage return after a specific character (in this case, an exclamation point). Let’s get to it!

EXAMPLE 1:

I have a folder on my desktop ready to go with all the files that I want to “search and replace”. After opening Notepad++, go to Search > Find in Files…

clip_image001

I want to find all instances of “s0/0/0” and replace those with “s0/0” …and I want to do so for all files in which this occurs:

clip_image002

You can see I’ve selected the directory that contains all the files and defined my criteria. If you’re confident then you can go ahead and click “Replace in Files”. If you’re a little cautious and want to review before making a commitment then click “Find All” which will bring up a list of all occurrences including what document each instance occurs in along with the line number:

clip_image003

Here you can see the search found 48 hits and the name of each document (along with the line number in that document the instance occurs.)

From here, right click in the results pane and “Select All”, right-click again and “Open All”. Now go back to Search > Replace (or use the toolbar icon / “ctrl+H”):

clip_image004

…And this time click Replace All in All Opened Documents:

clip_image005

From here you can go to > File > Save All then > File > Close All and that’s it!

EXAMPLE 2:

In this example I want to add a carriage return after each exclamation point occurrence within two files

clip_image006

The process is the same: Open Notepad++ > Search > Find in Files. This time I want to find “!” and Replace with a carriage return. The syntax for the return is \r\n and this time I want to select “Extended” under the Search Mode:

clip_image007

Again, I’m going to first “Find All” and do a review before I commit. The results are a total of 96 hits (or 96 instances where a “!” was found). Now I’m going to open the two files (in the results pane, right click > select all > open all) and do the replace in the same manor above. The outcome:

clip_image008

Now, why did I want to do that? Well a few days ago I opened a config file in Notepad and everything was on one line with only an exclamation point (“!”) to separate commands. This was in regular Notepad (not Notepad++) and the auto wrap feature just made it a mess. I knew the other 10 or so files would be the same so I remembered (Example 1) how I did several batch changes months earlier using Notepad++ and decided to do a blog / walk through. Well, as soon as I opened up one of the files in Notepad++ to review it, the formatting was perfect and I didn’t need to go through a tutorial with those files. Long story short, to finish this post (Example 2) I made-up some documents to use as reference. So why am I even mentioning all this? Well…

Bonus Lesson: If you’re working with config files that are written in a text editor and they don’t format well in regular Notepad, try Notepad++.

And that’s all I got to say about that;

-Prost!

No comments:

Post a Comment