1.4. The sed Editor

This section presents the following topics:

  • Conceptual overview of sed

  • Command-line syntax

  • Syntax of sed commands

  • Group summary of sed commands

  • Alphabetical summary of sed commands

1.4.1. Conceptual Overview

sed is a non-interactive, or stream-oriented, editor. It interprets a script and performs the actions in the script. sed is stream-oriented because, like many Unix programs, input flows through the program and is directed to standard output. For example, sort is stream-oriented; vi is not. sed's input typically comes from a file or pipe, but it can also be directed from the keyboard. Output goes to the screen by default but can be captured in a file or sent through a pipe instead.

The Free Software Foundation has a version of sed, available from ftp://gnudist.gnu.org/gnu/sed/sed-3.02.tar.gz. The somewhat older version, 2.05, is also available.

Typical uses of sed include:

  • Editing one or more files automatically

  • Simplifying repetitive edits to multiple files

  • Writing conversion programs

sed operates as follows:

  • Each line of input is copied into a "pattern space," an internal buffer where editing operations are performed.

  • All editing commands in a sed script are applied, in order, to each line of input.

  • Editing commands are applied to all lines (globally) unless line addressing restricts the lines affected.

  • If a command changes the input, subsequent commands and address tests will be applied to the current line in the pattern space, not the original input line.

  • The original ...

Get sed, awk and Regular Expressions Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.