DOWNLOAD THE CODE:
Download the Code 5697.zip

An indispensable batch-file utility

During the past 18 months of automating Windows NT Server installations, I discovered an indispensable batch-file utility that UNIX users have always known about—SED. This utility lets me do things that aren't possible or would be more difficult using standard batch-file commands. For example, SED simplifies taking input from a user at a command prompt, retrieving values from text files and assigning them to environment variables, and parsing text files for different stages of an automated installation. Let me introduce you to SED and show you several examples of how this utility can help you make your batch files accomplish more.

What Is SED?
SED is a UNIX editing tool that can now run in DOS and Win32 environments. SED's function is to take a stream of text from a file or the console and manipulate it. Taking input from the console lets you write scripts that prompt the user for input. SED redirects the user's input to a file that your batch files can access.

NT administrators have many advanced scripting languages at their disposal, but SED has one major advantage over these scripting languages—size. The DOS version of SED easily fits on a 3.5" disk. Although SED is available in a Win32 version, I've found that the DOS version is the most useful. The DOS version runs well in DOS and NT, and some Win32 versions of SED don't take user input correctly.

SED Syntax
SED commands are case sensitive and their syntax is a bit cryptic, but the time you invest in learning SED rewards you with more capable batch files. To ensure that scripts produce the results you want, you must be careful when writing scripts with SED. Always test SED commands that edit files to make sure they produce the correct results. Administrators can make syntax errors that delete a file's contents or damage system files to the point that their machine stops functioning. The best practice is to make a backup copy of the file before you use SED to edit the file. Figure 1 shows the simplified SED syntax.

Using Search and Replace
Let's start with simple search and replace examples to help you understand the syntax. The SED syntax requires one or more commands, an input source, and an output destination, although whether you supply the input and output parameters is optional. If you don't specify a file as the input source, SED takes the input from the console. If you don't redirect the output to a file, SED sends the results to the console. As you test SED commands, the fact that SED sends the result to the console is very helpful because it saves you from having to look inside an output file. For example, suppose the path to the input file is C:\Temp\ Logon.CMD and the input file contains the following lines:

NET USER X: \\US0031\FILES
NET USER Y: \\CA0107\GROUP
NET USER Z: \\US0237\USERS

In this batch file, the Net User commands need to be Net Use commands. To fix this error, use the following SED command, which reads the input file, replaces the Net User commands with Net Use commands, and outputs the result to the console:

SED "s/NET USER/NET USE/" <
   C:\Temp\Logon.CMD

This SED command uses the s function to substitute the string matching a pattern. Three forward slashes (/) always follow the s function, and you place the pattern you want to match between the first and second slashes. You place the string you want SED to substitute between the second and third slashes. Don't forget that SED is always case-sensitive.

Slightly modifying the previous command redirects the output to a new file:

SED "s/NET USER/NET USE/" <
   C:\Temp\Logon.CMD >
   C:\Temp\Logon2.CMD

This command redirects the output to the Logon2.CMD batch file. This step protects the original file from any syntax errors in the SED command that might produce undesirable results.

Next, suppose management decrees a new naming standard for all the servers in your organization. You need to replace your servers' two-character US country code with a three-character USA code. The following SED command reads the original batch file and changes all instances of US to USA:

SED "s/US/USA/g" <
   C:\Temp\Logon2.CMD

The g function (i.e., global function), which follows the third slash in this example, instructs SED to replace each occurrence of US, not just the first occurrence. However, this command isn't perfect; it changes the US to USA in all machine names, but it also replaces the US in USERS. To specify which occurrences of US you want to replace, you must modify the previous command:

SED "s/\\\\US/\\\\USA/g" <
   C:\Temp\Logon2.CMD

This modification uses four backslashes to tell SED to match the pattern of \\US and replace it with \\USA. The backslash character tells SED to treat the following character literally. To override the special meaning, you must precede all special characters with a backslash.

   Prev. page   [1] 2     next page
 
 

ADS BY GOOGLE