Other edit options
i - inserts a line into the output stream.
1 i\
text-to-insert
Text is sent directly to standard-out.
It will not be edited by other edit commands.
This is an insert, it will be inserted before the specified target line.
In example above, it will become the new line 1 in output.
There must be nothing but end of line following the back-slash.
A multi-line statement may be inserted by back-slashing the end of each
line except the last.
If used with a range, insert will occur for each line of range.
1 i\
text to insert\
more text\
and even more
Note : an inserted line is inserted directly into the output stream and,
therefor, cannot be edited by subsequent edit commands in the current
sed session.
a - appends a line into the output stream.
1 a\
text-to-append
Same rules as i except it is appended after the target line is
edited and sent to output.
Note : an appended line is appended directly into the output stream and,
therefor, cannot be edited by subsequent edit commands in the current
sed session.
c - change a line and send it to the output stream.
1 c\
new-text
Same rules as i except it replaces the target line.
Note :
When using a regular expression to match line, the whole line is
changed. Not the same as substitute.
When used with a range, whole range replaced by one occurrence of
of the new-text.
If you are planning on changing a line, don't bother with ANY other edits,
they will be discarded.
Note : changed lines are posted directly into the output stream and,
therefor, cannot be edited by subsequent edit commands in the current
sed session.
d - delete a line.
Deletes line. sed will immediately fetch a new line and re-start at
the first edit statement.
Any edit commands after the d will be ignored, as there is nothing
to edit.
If you wish to completely change a line to something else, use substitute.
If you wish to clear a line but leave it physically there,
either insert a blank line or use substitute.
s/^.*$//
p - print current line.
Sends current line/range in its current state to standard out.
Multiple prints will create multiple lines in output.
Usually used with -n, but can also be useful for debugging.
Can be appended on the substitute command.
# Output only the 5th-10th lines
sed -n "5,10p" data
# Output any line that is not a comment line.
sed -n "/^#/!p" pgm.cpp
# Output only portions of a web page that are in a preformatted block.
sed -n "//,/<\/pre>/p" web.html
w file-name - writes to specified file.
Sends current line/range in its current state to specified file.
Once a file is named and used, further writes are appended for the current
edit session.
Multiple files may be named. Various documentation claims a 10 file limit,
but this is most likely out of date.
I've successfully tested with 26 files.
Assume you have a file of expenses with the 1st column listing category.
sed -n -f "sorter" expenses
sorter contains :
/^Electric/w home
/^Gas/w home
/^Car/w car
/^Groceries/w food
y/match/replace/ - like the tr command.
Must specify every character to be matched and replaced.
Both match and replace are literals and have to be same length.
Does NOT recognize the [[:symbolic:]] or range a-z syntax.
Applied to every character on line.
Lines can be further edited.
n - get next line.
n - sends current line to standard out unless the -n option was used
on sed and reads in the next line.
sed will apply any remaining sed commands to the new line.
When sed gets to the end of the edit sequence, it read in the next
line and restarts edit sequence.
sed -e "n" -e "p" data
will cause the 2,4,6,etc. lines to be double printed.
The command sequence is NOT reset, so any additional commands defined are
applied to new line. But edit commands already processed before the n
are NOT available for the new line fetched.
Useful where lines are paired up.
Use an address filter to recognize the 1st line
Process it as desired.
Use n to fetch next line.
Process it as desired.
Assume you have a file that lists a topic on one line and definition on
next. All topics/definitions are line pairs.
The following basically outputs the topic line and indents the definition
line before outputting it.
q - quit.
q terminates the edit session.
Can be useful when there is a condition requiring editing to stop.
sed "/<\/html>/ q" doc.html > newdoc.html
This will output all lines up to and including the first line with
<\/html>.
Any text after the html end of document marker is not processed.
See the sed man page for additional commands. Some are GNU specific.