Shell practice: Introduction to the sed stream editor

Quick Edit

Sed (Stream EDitor) [1] automates repetitive operations on a text file and is especially effective when used in a shell script and with regular expressions (regex). In this article, I send program output to the screen. If you want to participate and practice, simply use the text files provided [2].

Sed Commands

The program calls up and accepts commands from virtually anywhere. You can pass in commands directly or read them in from a file. The data can be piped, redirected, or input from a text file. The output can be sent to the screen (usually stdout), through a pipe to the next command, or redirected to a destination file. (See the "Sed Call Options" box.) To resolve shell variables, you sometimes need to substitute the " for the ' character.

Sed Call Options

Sed simply reads the text file and returns the results through stdout:

sed [COMMAND] [TEXTFILE]

Same with input redirection:

sed [COMMAND] < [TEXTFILE]

Inclusion of sed in one or more pipes:

[PROGRAM1] | sed [COMMAND] | ......

Commands stored in a separate file and read in:

... sed -f [SCRIPT] .....

Output of sed redirected to a text file, but omitting error messages:

... sed [COMMAND] > [TARGETFILE]

The same, but including error messages in the target file:

... sed [COMMAND] > [TARGETFILE] 2>&1

Syntax

The basic syntax structure is shown in Figure 1.

...
Use Express-Checkout link below to read the full article (PDF).

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus