Skip to content

Sed Workflow

To effectively use sed (Stream Editor), it’s crucial to understand its internal workflow. This knowledge forms the foundation for writing efficient and predictable sed scripts. sed processes text line by line, following a consistent cycle for each line.

The sed workflow can be summarized in three main steps, repeated for each line of input:

  1. Read: sed reads one line from the input stream (which can be a file, output from another command via a pipe, or direct standard input) into a temporary storage area called the pattern space. If it’s the first line, the pattern space is initially empty. If there are no more lines to read, sed stops.
  2. Execute: All sed commands provided in the script are sequentially applied to the content of the pattern space. These commands might modify, delete, or add to the text in the pattern space. Commands are applied globally to all lines by default, unless specific line addressing (like line numbers or patterns) restricts their application.
  3. Display: After all commands in the script have been executed for the current line in the pattern space, sed typically prints the (potentially modified) contents of the pattern space to the standard output stream. This default printing behavior can be suppressed using the -n option. Once printed (or suppressed), the content related to this specific input line is typically cleared from the pattern space (unless commands like N, D are used which alter this flow for multi-line processing).

This Read-Execute-Display cycle continues until all lines from the input stream have been processed.

  • Pattern Space: This is sed’s primary workspace. It’s an in-memory buffer where the current line being processed is held. Most sed commands operate directly on the contents of the pattern space. The contents of the pattern space are volatile and are generally replaced or cleared as each new line is read (exceptions exist with advanced commands like N, P, D).
  • Hold Space: sed also provides another in-memory buffer called the hold space. Unlike the pattern space, the hold space is not directly operated upon by most commands, nor is its content automatically printed. Its contents persist across cycles, meaning data stored in the hold space remains available as sed processes subsequent lines. Special commands (h, H, g, G, x) are used to move data between the pattern space and the hold space. This allows for more complex operations, like remembering a previous line or rearranging blocks of text.
  • Input File Immutability (Default): By default, sed reads the input file but does not modify it directly. The transformations are applied to the data in the pattern space, and the result is sent to standard output. To modify the original file, you typically redirect the output of sed to a new file and then replace the original, or use the -i option (in-place editing) provided by GNU sed (use with caution, perhaps backing up the original first).
  • Standard Input (stdin): If no input filename is provided on the command line, sed reads from standard input. This allows sed to be used in pipelines, processing the output of other commands.
  • Global Application: If no address (line number or pattern) is specified for a command, sed applies that command to every line it reads.

Let’s create a simple text file named quote.txt:

$ echo -e "There is only one thing that makes a dream impossible to achieve: the fear of failure.\n - Paulo Coelho, The Alchemist" > quote.txt

Example 1: Simulating cat (displaying file contents)

$ sed '' quote.txt

Output:

There is only one thing that makes a dream impossible to achieve: the fear of failure.
- Paulo Coelho, The Alchemist

Workflow Breakdown:

  1. Read (Line 1): sed reads the first line (“There is only one thing…”) into the pattern space.
  2. Execute: The sed script is '' (empty, meaning no commands). So, no operations are performed on the pattern space.
  3. Display: sed prints the contents of the pattern space (the first line) to standard output.
  4. Read (Line 2): sed reads the second line (” - Paulo Coelho…”) into the pattern space, replacing the previous content.
  5. Execute: Again, no commands are applied.
  6. Display: sed prints the contents of the pattern space (the second line) to standard output.
  7. The process stops as there are no more lines.

Example 2: Reading from Standard Input

$ sed ''

Now, if you type a line of text and press Enter:

Hello, sed world!
Hello, sed world!

Explanation: The first “Hello, sed world!” is the line you typed (input). sed reads it, applies no commands (due to ''), and then prints it (output). To exit this interactive sed session, you typically press Ctrl+D (to signal End-Of-File).

Understanding this workflow is key to anticipating how sed will transform your text and to crafting more complex and powerful scripts.