Posts Tagged ‘Regular Expression’

Utility that Finds and Replaces String In File

Sunday, January 25th, 2009

This is a handy utility that finds and replaces a text string in one or more files using Perl regular expression.

I frequently use this to globally change text when I have many files that I need to change. Not long ago, I had 38 xsl attachments that I had to change because the path to my css changed. All the reorganizations are insane! Next change is to store the css as an Oracle artifact. Anyway,  I have half of my object in a weblogic container, but I decided to change those manually. Since the attachments are stored in Oracle as Blobs, I used my Download java program to extract the attachments to disk. Then I used this utility to change the 38 files to the new css file path. Then I used my Upload java program to load them from disk back to the Oracle Blob. This literally took 20 minutes to do, and since I did not have to rely on manually (i.e. fat fingering) doing the changes, there were no runs, no drips, and more importantly no errors!

You may ask why I did not use this method for the files in the container. There are approximately 200 files in the container. In case the pooch was injured (i.e. I screwed up), I have backups, for sure, but said backups are not easily obtained. How difficult you ask. Jumping through fire hoops after skinny dipping in a gasoline bubble bath comes to mind. I would not go so far as to say that I was concerned about the possibility of doing extra work, although the thought did cross my mind, but I really dislike fire and I absolutely dislike the backup custodian, and did not want to take a chance dealing with either.

Example:

Windows: perl -pi.bak -e “s/old-css-path/new-css-path/g” 1.xsl 2.xsl 3.xsl

Mac: perl -pi.bak -e ’s/old-css-path/new-css-path/g’ *.xsl

The ‘g’ modifier (at the end of the expression) will replace all occurrences in the file. Omitting the ‘g’ will only change the first occurrence. Include the ‘i’ modifier will do case-insensitive matching (find).

The above example (windows version) changes three files; 1.xsl, 2.xsl, 3.xsl in the current directory,  make a backups copy (i.e. 1.xsl.bak) of each file, and changes the string ‘old-css-path’ to ‘new-css-path’ if applicable.

Note: I could not get the wildcard ‘*’ to work in the windows environment. It seems to me that ‘*.xsl’ should work, but no such luck. You can stack them up as shown. Oh well, Windows lovers; you’re out of luck. It worked as advertised on my iMac running OS X . Go figure. Also, note that I have to use double quotes around the expression in windows. I received errors otherwise.

switches:
‘p’ - loops over code segment

‘i’ - specifies edit in place, including extension will make backup of file before changing

‘e’ - used to enter one or more lines of script in command line