Using a shell script to replace text in multiple files
January 22nd, 2005 1 Comment
The problem
I usually work at the office, but occasionally work from home. At the office I can browse to any networked server in Windows (e.g. in Windows Explorer) by entering an address like \\networkedserver. When I'm working from home, I log on to my work network via an SSH session. This works fine, but one drawback is that, because it's not a VPN session my home PC is not part of the work network, so although I can access machines from a command line, using SSH, or upload/download files using WinSCP, addresses like \\networkedserver don't work because (unless "networkedserver" happens to be one of my own machines on my own home network).
How this affects my work
The only hassle this causes is when I work on structured FrameMaker files from home. I need to copy the source XML files from the server at work to my own machine (using WinSCP) and then modify them the path to the DTD, using a text editor. Once I've finished working on them in FrameMaker and I want to copy them back to the work server, I need to change the DTD reference back to one that works on my work network.
For example, at work the FrameMaker XML files I work with reference a DTD called mxDocBook.dtd. Let's say the path to this is //networkserver/mxDocBook/mxDocBook-DTD/mxDocBook.dtd. So the start of each XML file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter SYSTEM "//networkserver/mxDocBook/mxDocBook-DTD/mxDocBook.dtd" [
...
If I tried to open this file in FrameMaker on a computer on my home network I'd get an error message, because FrameMaker wouldn't be able to find the DTD file. So I have to change the identifier to something like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter SYSTEM "file:///D:/work/mxDocBook/DTDfiles/mxDocBook.dtd" [
...
Changing each file back and forwards could get very tedious, but there's a solution.
The solution: the changedtd shell script
I wrote a shell script that you can use to automate this process. The script (called changedtd) contains variables called FINDTHIS, CHANGETOTHIS and FNAMEPATTERN. It looks for files in the current working directory that match the FNAMEPATTERN (typically *.xml) and performs a search/replace, searching for FINDTHIS and replacing it will CHANGETOTHIS.
If you want to use the script, copy it and edit the FINDTHIS and CHANGETHIS values to appropriate paths.
To run the shell script you obviously need a shell. I'd recommend installing Windows Services for UNIX 3.5 as the easiest way of getting a UNIX-like shell on a Windows machine.
You can download SFU 3.5 free of charge from:
www.microsoft.com/windows/sfu
Once you've got a shell, all you need to do is put the changedtd file in a directory that's in your PATH, and then, in a UNIX/SFU shell, browse to the directory containing the files you want to change and run the command: changedtd
View/download the script
- View the script
- To download the script, right-click this link and choose Save Target As or Save Link As.
Notes:
You might think it's a very long-winded script to do such a simple operation, but most of the verbosity of it comes from:
a) The parsing of the command line arguments. This allows you to run the script on a directory other than the current working directory and to recurse into child directories (if you dare).
b) The usage instructions, which you can read by running changedtd -h.
c) You could of course very easily change this script to do a search/replace with any text you like on any type of file, just by changing the values of FINDTHIS, CHANGETOTHIS and FNAMEPATTERN.
Potentially similar posts
- Scan for available IP addresses – August 2011
- My first (useful) Ruby program – June 2011
- Adding your choice of text editor to Flare’s Open With menu – December 2010
- Perl basics for beginners (on Windows) – August 2010
- Gotchas with running a Perl script as a cron job – August 2010
July 14th, 2011 at 7:19 pm (#)
Hi, i wanted to see the file 'cause I need to do the same. The only change is that I use Windows and I need to replace variables in a xml.
For example I need to replace this variable @QuartzJars@ in a xml file by this string in a txt file: E:/SonicDataFiles/AR_INT/GPP_VE/IN
Could you please upload the script again?
Thanks in advance!
Regards
Gerardo