sed Command Reference

The sed (Stream Editor) command is a powerful tool for performing text transformations and manipulations on streams of text, such as files or input from pipelines. This reference provides commonly used sed commands and examples to help you efficiently edit and process text files.

Basic Syntax

The general syntax for using sed is:

sed [options] 'command(s)' file(s)

Or for inline commands:

sed [options] -e 'command1' -e 'command2' file(s)

Commonly Used Options

Option Description
-e 'command' Add the script to the commands to be executed.
-f scriptfile Execute the script in the specified file.
-i[SUFFIX] Edit files in-place (optionally creating a backup with the given suffix).
-n Suppress automatic printing of pattern space.
-r or -E Use extended regular expressions in the script.
-s Treat files as separate rather than as a single continuous long stream.

Basic Commands

Command Description
s/old/new/flags Substitute new for the first occurrence of old in a line.
p Print the current pattern space.
d Delete the pattern space; start next cycle.
q Quit sed; no further processing.
a\ text Append text after the current line.
i\ text Insert text before the current line.
c\ text Replace the current line with text.
n Read next line into pattern space.
h Copy pattern space to hold buffer.
g Get hold buffer content and replace pattern space.
G Append hold buffer content to pattern space.
x Exchange the pattern space with the hold buffer.
= Print the current line number.

Flags for Substitution Command (s///)

Flag Description
g Global replacement in a line (replace all occurrences).
p Print the line if a replacement was made.
w file Write the line to file if a replacement was made.
I Case-insensitive search.
n Replace the n-th occurrence in a line.

Addressing

Addresses specify which lines of input to apply commands to.

Common Examples

Command Description
sed 's/old/new/' file Replace first occurrence of old with new in each line.
sed 's/old/new/g' file Replace all occurrences of old with new in each line.
sed -i 's/old/new/g' file Edit the file in-place, replacing all occurrences.
sed '/pattern/d' file Delete lines matching pattern.
sed -n '/pattern/p' file Print only lines matching pattern.
sed -n '5,10p' file Print lines from 5 to 10.
sed '1d' file Delete the first line.
sed '$d' file Delete the last line.
sed '1i\New Header' file Insert "New Header" at the beginning of the file.
sed 's/^/prefix_/' file Add "prefix_" at the beginning of each line.
sed 's/$/_suffix/' file Add "_suffix" at the end of each line.
sed 's/^\(.\{5\}\)/\1.../' file After first 5 characters of each line, insert "...".
sed -n '=' file Print line numbers.
sed '/^$/d' file Delete empty lines.
sed 's/\(.*\)/\U\1/' file Convert text to uppercase.
sed 's/\(.*\)/\L\1/' file Convert text to lowercase.
sed 's/^\s\+//' file Remove leading whitespace.
sed 's/\s\+$//' file Remove trailing whitespace.

Multi-Line Operations

By default, sed operates on a per-line basis. To perform multi-line operations, use the following commands:

Example: Join Every Two Lines

sed 'N;s/\n/ /' file

This reads the next line into the pattern space and replaces the newline character with a space, effectively joining every two lines.

Regular Expressions in sed

sed supports regular expressions for pattern matching:

Hold Space Operations

sed provides a hold space (buffer) for advanced text processing:

Example: Swap Two Lines

sed 'N;swap_lines' file

# Define the swap_lines script:
swap_lines='
    h       # Copy pattern space (line1\nline2) to hold space
    s/.*\n//    # Remove line1, keep line2 in pattern space
    x       # Exchange pattern space and hold space
    s/\n.*//    # Remove line2, keep line1 in pattern space
    G       # Append hold space (which now contains line2) to pattern space
'

Tips and Best Practices

Return to Home