sed (target)

Stream editor (POSIX, toybox)

Syntax:

sed [-Einrsz] [-e script]... | script [-f script_file]... [file...]

Runs on:

QNX OS

Options:

-E
POSIX alias for -r
-e script
Add the specified script to the list.
-f script_file
Add contents of the file script_file to the list.
-i
Edit each file in place (-iEXT keeps backup file with extension EXT).
-n
No default output (use the p command to output matched lines).
-r
Use extended regular expression syntax.
-s
Treat input files separately (implied by -i).
-z
Use \0 rather than \n as input line separator.
file
The pathname of a file to use as input.

Description:

The sed utility is a stream editor that applies editing scripts to its lines of input.

Scripts

A script is one or more commands separated by newlines or semicolons. All -e options are combined as if they're separated by newlines, followed by all -f options. If -e or -f is not used, then the first argument is the script.

Commands apply to every line unless they are prefixed with an address of the form:

[address[,address]][!]command

Addresses

An address is one of the following:

  • a line number (starting at 1)
  • a regular expression
  • a $ for the last line of input (-s or -i makes it the last line of each file)

One address matches one command line. Two addresses matches from the first to second command line inclusively. Two regular expressions can match multiple ranges. An address to the Nth address ends N lines later.

You can use the negation character (!) to invert the match.

Regular Expressions

The sed utility uses regular expressions that start and end with the same character (anything except the backslash (\) or newline character). You can use the delimiter in the regex by escapeing it with a backslash. Using printf to escape (\abcefnrtv octal, hex, and unicode) also works. An empty regex repeats the previous one. Address regexes require any first delimiter (except the forward slash (/)) to be escaped to distinguish it from the specified commands.

Commands

The sed utility reads each line of input, processes it, and then writes it out or discards it before reading the next. The sed utility can remember one additional line in a separate buffer (using the h, H, g, G, and x commands) and read the next line of input early (using the n and N commands). Otherwise, it operates on individual lines.

Each command starts with a single character. The commands with no arguments are:

!
Run this command if the specified address didn't match.
#
Comment; ignores rest of this line of the script (until newline).
{
Start new command block, continuing until a corresponding }. Command blocks nest and can have addresses applying to the whole block.
}
End command block (this command cannot have an address).
=
Print the current line number (plus newline).
D
Delete one line of input and restart the specified command script (same as d unless you've glued lines together with N or a similar command).
d
Delete this line and move on to the next one (ignores remaining commands).
G
Get remembered line and append the current line.
g
Get remembered line and overwrite the current line.
H
Remember this line and append to remembered line, if any.
h
Remember this line and overwrite the remembered line.
l
("el") Print line escaping \abfrtvn, octal escapes for other nonprintng chars, wrap lines to terminal width with \, and append $ to end of line.
N
Append \n and the next line of input to this line. Quit at EOF without default output. Advances line counter for the address and =.
n
Print default output and read next line over current line (quit at EOF).
P
Print this line up to first newline (from N).
p
Print this line.
q
Quit (print default output; no more commands processed or lines read).
x
Exchange this line with remembered line (overwrite in both directions).

The commands that take an argument are:

: label
Target for jump commands.
a text
Append text to output before reading next line.
b label
Branch; jumps to : label (with no label to the end of script).
c text
Delete matching address range and output text instead.
i text
Insert text (output immediately).
r file
Append contents of file to output before reading next line.
s/S/R/F
Search for the specified regular expression S to replace a match R using the specified flags F. The delimiter is anything but \n or \. You can use \ or printfto escape in S or R. An unescaped "&" in R becomes a full matched text, \1 through \9 represents the parenthetical subexpression from S. A \ at end of a line will append to the next line of the script.

The flags in F are:

N
N=0 to 9. Substitute for the Nth match.
g
Globally substitute all matches.
i or I
Ignore case-sensitivity when matching.
p
Print resulting line when a match is found and replaced.
w [file]
Write (append) line to file when a match is replaced.
t label
Test, jump if s/// command matched this line since last test.
T label
Test if false, jump to : label only if no s/// found a match.
w file
Write (append) line to file.
y/old/new
Change each character in old to the corresponding character in new (with standard backslash escapes and delimiter can be any repeated character except \ or \n).

The text arguments (for a, c, i) may end with an unescaped backslash character (\) to append to the next line (leading whitespace is not skipped). It may also treat the semicolon (;) as a literal character (use \; instead).

This utility is provided as part of the toybox package. For information on how to enable it, see toybox.

Contributing author:

Rob Landley and the toybox project (see https://landley.net/toybox/).

Page updated: