Subscribe to Windows IT Pro
August 01, 1999 12:00 AM

SED

Windows IT Pro
InstantDoc ID #5697
Rating: (1)
Downloads
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/" 

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\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" 

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" 

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.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    8 years ago
    Nov 30, 2004

    How do I insert a new line in a search and replace statement?

  • LH
    8 years ago
    Jun 15, 2004

    Better get the version from
    http://unxutils.sourceforge.net.

    The ones here do not substitute \\1, \\2, \\n, \\t, etc properly. like so:
    s/example/for example:\\n\\t/
    s/^default=.*partition(\\([0-9]*\\)).*$/SET PARTNUM=\\1/

    Then you can reduce a lot of code.

  • james santinello
    9 years ago
    Sep 16, 2003

    i am a little new to some of this but exactly what is meant by "machine path directory".also can this be used with a
    WIN98 system?any particulars would be appreciated.thanx

  • simon
    9 years ago
    Jun 05, 2003

    SED is an ancient *nix utility. Open source.
    Maybe ya should do some of your own research before posting?

    what I WOULD like to know is where the binary is. While I'm sure I can find it myself, a linke to where you got the binary from would be very helpfull.

  • Jeffrey Cooke
    9 years ago
    Apr 11, 2003

    Need to know the source of the code (who wrote it) and redistributions rights before this utility is useful ...

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.