Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

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. The g 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”.
  • 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.
  • 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”.
  • 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 as sed '3a New Line'
  • i <text> inserts <text> before the current line.
    • e.g.: sed '1i\Header' inserts “Header” before line 1.
  • c <text> changes the current line to <text>.
    • e.g.: sed '/old/c\new content' replaces lines matching “old” with “new content”.
  • y/<src-chars>/<dest-chars>/ transliterates characters one-to-one (like tr).
    • e.g.: sed 'y/abc/ABC/' converts ‘a’→‘A’, ‘b’→‘B’, ‘c’→‘C’.
  • q [<exit-code>] quits processing immediately. With address, exits after processing that line.
    • e.g.: sed '5q' processes only the first 5 lines.
  • 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”.
  • w <file> writes the current pattern space to <file>.
    • e.g.: sed '/important/w log.txt' saves lines with “important” to “log.txt”.
  • = prints the current line number to stdout.
    • e.g.: sed -n '/pattern/=' prints line numbers containing “pattern”.
  • n next line: replaces pattern space with next input line, skip remaining commands.
    • e.g.: sed 'n;d' deletes every *even*-numbered line.
  • 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.
  • P prints the first part (up to newline) of the pattern space. Used after N.
    • e.g.: sed -n 'N;P' prints only the first line of each pair.
  • 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.
  • 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.
  • x exchanges hold space and pattern space.
    • e.g.: sed '/swap/{x;p;x}' prints hold space when “swap” is matched.
  • l prints pattern space in unambiguous form (non-printable as hex, wraps long lines).
    • e.g.: sed -n l shows hidden characters.
  • : <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 or t.
    • e.g.: sed ':a; s/foo/bar/; ta; s/baz/qux/' repeatedly replaces “foo” until none remain, then replaces “baz”.
  • 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.
  • F prints the filename being processed (useful with multiple files).
    • e.g.: sed -n F *.log prints all filenames in a directory.
  • Q [<exit-code>] quits without printing pattern space (like q but silent).
    • e.g.: sed '/match/Q42' exits with code 42 upon first “match”.
  • v <version> fails if sed 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.
  • # denotes a comment until next newline.
    • e.g.: sed 's/A/B/ # Replace A' ignores text after #.
  • { } 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”.
  • T <label> branches to <label> only if no substitutions succeeded since last input line or T (opposite of t).
Notes
  1. Most commands accept addresses (e.g., <start>,<end> or /<regex>/) to restrict operation.
  2. a, i, and c 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.
  3. Hold space commands (h, g, etc.) enable advanced stateful operations.
  4. 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!
  • <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.
  • <start>,<end> operates on lines from start to end (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.
  • ! 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.
  • <address>,+<n> selects address plus n 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
  • <start>~<step> selects every step lines starting at start (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...
Summary Table of sed Address Types
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”