Sed/Syntax
From atl.wiki
More actions
For the main page, visit sed.
The general syntax in sed is:
<address#1>{<command#1>; <command#2>; ...}
While at least 1 command is needed, addresses are optional; specifying no commands does not alter the text stream in most cases. If a filter corresponds to only 1 command, the braces can be omitted:
<address#1><command#1>
To specify multiple commands, separate them with new lines. Most commands also allow separation by semicolons (;
), but greedy commands such as c
and i
take in the semicolons and the rest of the line, and thus cannot be separated with semicolons.
Commands
s/<regex>/<replacement>/<flags>
performs substitution. Theg
flag replaces all matches per line; without it, only the first match is replaced. Other flags:\1,\2,...
(The n-th regex group),p
(print on replace),i
(case-insensitive),w <file>
(write to file).- e.g.:
sed 's/red/blue/g'
replaces all “red” with “blue” (including in “Fred” → “Fblue”). - e.g.:
sed 's:\bdog\b:cat:g'
replaces whole words “dog” with “cat”.
- e.g.:
d
deletes the current pattern space. Often used with addresses.- e.g.:
sed '/error/d'
deletes lines containing “error”. - e.g.:
sed '3,7d'
deletes lines 3 to 7.
- e.g.:
p
prints the current pattern space. Usually combined with-n
to suppress automatic printing.- e.g.:
sed -n '/important/p'
prints only lines containing “important”.
- e.g.:
a <text>
appends <text> after the current line. Text continues until a newline not escaped with\
.- e.g.:
sed '3a\New Line'
appends “New Line” after line 3. This is the same assed '3a New Line'
- e.g.:
i <text>
inserts <text> before the current line.- e.g.:
sed '1i\Header'
inserts “Header” before line 1.
- e.g.:
c <text>
changes the current line to <text>.- e.g.:
sed '/old/c\new content'
replaces lines matching “old” with “new content”.
- e.g.:
y/<src-chars>/<dest-chars>/
transliterates characters one-to-one (liketr
).- e.g.:
sed 'y/abc/ABC/'
converts ‘a’→‘A’, ‘b’→‘B’, ‘c’→‘C’.
- e.g.:
q [<exit-code>]
quits processing immediately. With address, exits after processing that line.- e.g.:
sed '5q'
processes only the first 5 lines.
- e.g.:
r <file>
reads a file and appends its content after the current line.- e.g.:
sed '/End/r data.txt'
inserts contents of “data.txt” after lines containing “End”.
- e.g.:
w <file>
writes the current pattern space to <file>.- e.g.:
sed '/important/w log.txt'
saves lines with “important” to “log.txt”.
- e.g.:
=
prints the current line number to stdout.- e.g.:
sed -n '/pattern/='
prints line numbers containing “pattern”.
- e.g.:
n
next line: replaces pattern space with next input line, skip remaining commands.- e.g.:
sed 'n;d'
deletes every *even*-numbered line.
- e.g.:
N
appends next line to pattern space (with embedded newline). Useful for multiline processing.- e.g.:
sed 'N;s/\n/ /'
joins every two consecutive lines.
- e.g.:
P
prints the first part (up to newline) of the pattern space. Used afterN
.- e.g.:
sed -n 'N;P'
prints only the first line of each pair.
- e.g.:
D
deletes the first part (up to newline) of the pattern space. Restarts cycle if content remains.- e.g.:
sed 'N;/error/D'
deletes leading lines in a pair if “error” is found.
- e.g.:
h
copies pattern space to hold space (overwriting).H
appends with newline.g
copies hold space to pattern space (overwriting).G
appends with newline.- e.g.:
sed -n '/start/{h; /end/{g; p}}'
prints lines between “start” and “end” inclusively.
- e.g.:
x
exchanges hold space and pattern space.- e.g.:
sed '/swap/{x;p;x}'
prints hold space when “swap” is matched.
- e.g.:
l
prints pattern space in unambiguous form (non-printable as hex, wraps long lines).- e.g.:
sed -n l
shows hidden characters.
- e.g.:
: <label>
defines a label for branching. Requires a single colon.b <label>
branches to <label> (or end of script if no label).t <label>
branches to <label> only if a substitution succeeded since last input line ort
.- e.g.:
sed ':a; s/foo/bar/; ta; s/baz/qux/'
repeatedly replaces “foo” until none remain, then replaces “baz”.
- e.g.:
e [<command>]
executes a command and sends output to output stream. Without argument, executes pattern space.- e.g.:
sed 's/.*/date +%s/e'
replaces each line with the current Unix timestamp.
- e.g.:
F
prints the filename being processed (useful with multiple files).- e.g.:
sed -n F *.log
prints all filenames in a directory.
- e.g.:
Q [<exit-code>]
quits without printing pattern space (likeq
but silent).- e.g.:
sed '/match/Q42'
exits with code 42 upon first “match”.
- e.g.:
v <version>
fails ifsed
version is less than <version> (e.g.,v 4.2
).
z
zaps (empties) the pattern space. Useful for multiline processing.- e.g.:
sed '/pattern/{z;d}'
deletes lines after “pattern” is found.
- e.g.:
#
denotes a comment until next newline.- e.g.:
sed 's/A/B/ # Replace A'
ignores text after#
.
- e.g.:
{ }
groups commands into a block. Must appear as separate commands.- e.g.:
sed '/start/{s/A/B/; s/C/D/}'
applies substitutions only to lines containing “start”.
- e.g.:
T <label>
branches to <label> only if no substitutions succeeded since last input line orT
(opposite oft
).
- Notes
- Most commands accept addresses (e.g.,
<start>,<end>
or/<regex>/
) to restrict operation. a
,i
, andc
are “greedy” — text arguments span lines until an unescaped newline.- To use multiple commands, separate each command with a new line. Input a new line in a shell by pressing Esc and then ↵ Enter separately.
- Hold space commands (
h
,g
, etc.) enable advanced stateful operations. - Frequency ranking is estimated based on common use cases (e.g.,
s
is most frequent).
Addresses
/<regex>/
selects lines matching a regular expression pattern. Used to filter lines for command application.- e.g.:
sed -n '/error/p'
prints lines containing “error”. - e.g.:
sed '/^#/d'
deletes comment lines starting with#
. - In case a different delimiter needs to be used, the starting character needs to be escaped by a backslash like so:
\!/usr/include/[^/]+\.h!
- e.g.:
<n>
targets a specific line number.- e.g.:
sed '42s/old/new/'
replaces “old” with “new” only on line 42. - e.g.:
sed '1i\Header'
inserts “Header” before line 1. 0
and$
refers to the start and the end of the file.
- e.g.:
<start>,<end>
operates on lines fromstart
toend
(inclusive). Numbers, regexes, or$
(EOF) allowed.- e.g.:
sed '10,20d'
deletes lines 10–20. - e.g.:
sed '5,$s/foo/bar/'
substitutes “foo” → “bar” from line 5 to EOF. - e.g.:
sed -n '/error/,$p'
prints from first “error” to EOF.
- e.g.:
!
applies commands to lines NOT matching the address.- e.g.:
sed '/pattern/!d'
deletes lines not containing “pattern”. - e.g.:
sed '3,5!s/foo/bar/'
substitutes “foo” → “bar” except on lines 3–5.
- e.g.:
<address>,+<n>
selects address plusn
following lines (GNU Extension)- e.g.:
sed '/START/,+3d'
deletes “START” line + next 3 lines - e.g.:
sed '10,+5s/foo/bar/'
substitutes in lines 10–15
- e.g.:
<start>~<step>
selects everystep
lines starting atstart
(GNU Extension)- e.g.:
sed '1~2d'
deletes every odd line (1,3,5...) - e.g.:
sed '2~5s/^/# /'
comments lines 2,7,12,17...
- e.g.:
Type | Syntax | Example | Explanation |
---|---|---|---|
Regex | /pattern/
|
sed '/foo/d'
|
Delete lines containing “error” |
Single Line | 5
|
sed '5q'
|
Quit after line 5 |
Regex Range | /a/,/b/
|
sed '/start/,/end/p'
|
Print between markers |
Line Range | 3,7
|
sed '3,7s/a/b/'
|
Substitute in lines 3–7 |
Step | start~step
|
sed '1~3d'
|
Delete every 3rd line starting at 1 |
Offset | addr,+n
|
sed '/err/,+2d'
|
Delete error line + next 2 lines |
Special | 0 , $
|
sed '$s/old/new/'
|
Substitute in last line |
Combined | 5,/x/
|
sed '5,/end/d'
|
Delete from line 5 to next “end” |
Negation | !
|
sed '/keep/!d'
|
Delete lines not containing “keep” |