Command-line search and replace

Imagine that you recently completed a huge task—you consolidated several servers. A monster server named saturn that has RAID, clustering, and so on now contains the shares and applications formerly on atlas, redstone, and titan. Just as you finish the job, a chilling thought occurs to you: the logon scripts. You have hundreds of logon scripts, each with hard-wired references to atlas, redstone, and titan.

You pull up one of the scripts and take a look. The script contains lines such as net use v: \\atlas\pix and \\redstone\desktopapp\vscan c: d: e:. When you moved the shares to saturn, you retained all their original names. So the solution is simple: Just edit all the logon scripts and change all the atlas, redstone, and titan references to saturn. Although this task is simple to state, completing it would be torture. However, computers are good at this kind of repetitive task. And munge.exe is the tool you need to automate such a task.

Munge is a Microsoft Windows NT Server 4.0 Resource Kit command-line search-and-replace tool. To use the tool, create an ASCII script file that lists the changes you want to make. Then, feed Munge the script file and the file you want to change. Munge makes the changes you request and writes the changes to a file with the original file's name. To be safe, the tool keeps the original data file but changes its extension to .bak.

Suppose you have a file called animals, and you want to change every instance of cat in the file to dog. You need to use Notepad or another ASCII text editor to create a script file (e.g., called changes) with the line

cat dog

Then, invoke Munge by entering

munge changes animals

Munge reports all the changes it made, then exits. The animals file then contains those changes, and you have a file on your hard disk named animals.bak that contains the contents of the animals file before you ran Munge. (In case you're wondering, if you ask Munge to perform search-and-replace operations on files with the extension .bak, the tool complies but stumbles over a bug: Munge changes your modified file's extension to .mge and erases the original .bak file.)

If all your logon batch files were in one directory, you could use Munge with an old batch trick, the For...Do command, to complete the search-and-replace operations. First, create the script file (e.g., called srvchg) with the lines

atlas saturn
redstone saturn
titan saturn

for the three change commands. Then, rather than using Munge on each file (i.e., munge srvchg filename), use the For...Do command as follows to tell NT to complete the search and replace on a group of files:

for %f in (*.bat) do munge srvchg %f

For each file with the .bat extension in the current directory, the statement finds the file, constructs the command munge srvchg %f (substituting the file's name for %f), and tells NT to execute the command. Then, the For...Do statement repeats the cycle for the next .bat file in the directory. The For...Do statement finally stops when it runs out of .bat files.

Munge has a few quirks. For example, if your script file contains nonalphanumeric values (e.g., \\atlas \\saturn), Munge requires that you surround each item with quotes. Thus, you'd type

"\\atlas" "\\saturn"

This requirement creates problems with case. You want the search and replace to work whether the server name is atlas, ATLAS, or something in between. When you specify before and after values without using quotes, Munge is case-insensitive. But when you surround the before value with quotes, Munge is case-sensitive. In this case, the tool would change only \\atlas (not \\ATLAS) to \\saturn.

Munge's online Help discusses several options that I couldn't get to work. And unfortunately, this Help is the only documentation available. Although Munge is fairly buggy, it's a handy tool if you use it for the kind of tasks in my examples.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.