2.1. What is sed?
"sed" stands for Stream EDitor. Sed is a non-interactive editor,
written by the late Lee E. McMahon in 1973 or 1974. A brief history
of sed's origins may be found in an early history of the Unix
tools, at <https://www.columbia.edu/~rh120/ch106.x09>.
Instead of altering a file interactively by moving the cursor on
the screen (as with a word processor), the user sends a script of
editing instructions to sed, plus the name of the file to edit (or
the text to be edited may come as output from a pipe). In this
sense, sed works like a filter -- deleting, inserting and changing
characters, words, and lines of text. Its range of activity goes
from small, simple changes to very complex ones.
Sed reads its input from stdin (Unix shorthand for "standard
input," i.e., the console) or from files (or both), and sends the
results to stdout ("standard output," normally the console or
screen). Most people use sed first for its substitution features.
Sed is often used as a find-and-replace tool.
sed 's/Glenn/Harold/g' oldfile >newfile
will replace every occurrence of "Glenn" with the word "Harold",
wherever it occurs in the file. The "find" portion is a regular
expression ("RE"), which can be a simple word or may contain
special characters to allow greater flexibility (for example, to
prevent "Glenn" from also matching "Glennon").
My very first use of sed was to add 8 spaces to the left side of a
file, so when I printed it, the printing wouldn't begin at the
absolute left edge of a piece of paper.
sed 's/^/ /' myfile >newfile # my first sed script
sed 's/^/ /' myfile | lp # my next sed script
Then I learned that sed could display only one paragraph of a file,
beginning at the phrase "and where it came" and ending at the
phrase "for all people". My script looked like this:
sed -n '/and where it came/,/for all people/p' myfile
Sed's normal behavior is to print (i.e., display or show on screen)
the entire file, including the parts that haven't been altered,
unless you use the -n switch. The "-n" stands for "no output". This
switch is almost always used in conjunction with a 'p' command
somewhere, which says to print only the sections of the file that
have been specified. The -n switch with the 'p' command allow for
parts of a file to be printed (i.e., sent to the console).
Next, I found that sed could show me only (say) lines 12-18 of a
file and not show me the rest. This was very handy when I needed to
review only part of a long file and I didn't want to alter it.
# the 'p' stands for print
sed -n 12,18p myfile
Likewise, sed could show me everything else BUT those particular
lines, without physically changing the file on the disk:
# the 'd' stands for delete
sed 12,18d myfile
Sed could also double-space my single-spaced file when it came time
to print it:
sed G myfile >newfile
If you have many editing commands (for deleting, adding,
substituting, etc.) which might take up several lines, those
commands can be put into a separate file and all of the commands in
the file applied to file being edited:
# 'script.sed' is the file of commands
# 'myfile' is the file being changed
sed -f script.sed myfile # 'script.sed' is the file of commands
It is not our intention to convert this FAQ file into a full-blown
sed tutorial (for good tutorials, see section 2.3). Rather, we hope
this gives the complete novice a few ideas of how sed can be used.