
Lead Image © Dmitriy Sladkov, 123RF.com
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.
...Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
