4.13 sed -- A Stream Editor |
sed
is a text editor. But, it is
NOT an interactive text editor. You need to decide exactly what
and how you are going to edit prior to getting started. If you
have multiple files that need the same changes it is a good choice.
If you only have one file and the changes are complex, find
another editor -- sed is not your answer.
sed works by reading one input line into a buffer, referred to as "pattern space", processing all of the commands related to that line, normally printing the resulting line, then reading the next line
In addition to the standard output, results can be directed to several files. Additional input can be read from files and from the program.
In addition to the "pattern space" there is a "hold space".
Invoking sed:
sed [-n] [-e script] [-f sfile] [file...]
-n
is used to suppress the printing of lines after processing,
normally used in conjunction with the p
(print) command.
-e
is used to indicate that the following argument is the program.
This is normally optional.
-f
is used to indicate the file that contains the text processing
program.
The input to sed
, by default, comes from the standard input.
If a file name(s) is provided the input comes from the file(s).
Output from sed
goes to the standard output. It does not modify
the input file(s).
Addresses and Commands:
[address [, address] ] command [arguments]
An address is either a line number,
a "$" (the last line), or a context
address, "/regular expression/",
in the style of ed
.
1,$ All lines 32 Line 32 /Some/ lines with the string Some /Start/,/End/ Lines, inclusive, starting with a line containing Start and ending with a line containing EndCommands are one character following an optional address.
s/regular expression/replacement/flagsIf the replacement contains an "&" it represents the matched regular expression. flags
g
-- global; p
-- print after replacement;
w
wfile - write to file wfile if replacement was made.
Write ap
Print the current "pattern space".q
Quit processing.d
Delete the pattern space. Do not print.a\
Append text to output prior to reading next line. texti\
Insert text to output. texty/abcd/ABCD/
Transform any matching character in the first string to the corresponding character in the second string. Strings must be same length.
sed
script/program that changes:
~cis137/sed.text
from:
Tkis is the input to the sed lab. It is in a file in ~cis137/sed.text on system gort Tkis line hax the nmbers 123456789 and 0 Tkis line hax the nmbers 154374830 and 2 And tkis line too.to:
This is the input to the sed lab. It is in a file in ~cis137/sed.text on system lt.tucson.az.us This line has the numbers 234567890 and 1 This line has the numbers 265485941 and 3 And this line too.Turn in this script program marked with:
your-name TABER CIS137 Lab 4.13: sed program
4.13 sed -- A Stream Editor |