Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


August 1999

SED


RSS
Subscribe to Windows IT Pro | See More Task Automation Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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.

   Previous  [1]  2  Next 


Top Viewed ArticlesView all articles
Battery Life Issues Almost Certainly Not Windows 7's Fault

While Microsoft is still investigating a notebook battery life issue that was supposedly caused by Windows 7, some interesting trends have emerged. ...

Confirmed: Battery Life Issues Not Windows 7's Fault

Microsoft on Monday issued a lengthy statement about the recent Windows 7 battery controversy, echoing my assessment from earlier in the day, but backing it up with hard, cold evidence. ...

Microsoft Warns of Windows Version Expirations

Microsoft warned that this year will see three out-of-date Windows versions slip into retirement. ...


Task Automation Whitepapers Three Ways to Prevent Insider Risk within Your SCCM-Managed Environment

From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2010 Penton Media, Inc. Terms of Use | Privacy Statement