Thursday, June 21, 2012

Brute force using VIM

I was just presented with a situation where about 60 html files were generated and some of them contained duplicate entries that needed to be removed. We couldn't go back and re-generate because some of the verbiage on the pages had been tweaked already. I started looking into sed to see if it would help, but it apparently doesn't shine at finding duplicates and removing them. So it looked like the only way was to brute force edit each page ... blah!

But wait ... VIM can make even the most laborious task seem trivial if you know a few key concepts.

So I decided to buffer all the *.html files and tell vim to find a specific string in each of them. The string was the html class of the menu.

vim +/search\ string *.html

Now I have all the files open and I simply need to review each one for the dupes. So for each file, I simply pressed "n" to find the next occurrence. If the dupe was present, I executed


move down 7 lines, start selection, move down 6 lines then delete the selection. Then I simply saved the file and deleted it from the buffer and on to next file.

I probably could have used a macro but that would have taken longer to learn than the 10 minutes it took to edit the files. So I'll put that at the top of my reading list.

Knowing your toolset can make the most laborious task seem trivial. Continuous learning, not just the language you use but the tools that support your efforts, is the key here.