Catalog / Sed Text Manipulation Cheatsheet

Sed Text Manipulation Cheatsheet

A comprehensive cheat sheet for the `sed` stream editor, covering essential commands, syntax, and use cases for text manipulation.

Basic Usage

Syntax

sed [options] 'command' file

Where:

  • options: Modify the behavior of sed.
  • command: Instructions to apply.
  • file: Input file(s) to process.

Common Options

i

In-place editing (GNU sed). Requires no extension or an empty string as an argument (BSD).

Example:
sed -i 's/foo/bar/' file.txt

e

Add the script to the commands to be executed.

Example:
sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt

n

Suppresses automatic printing of pattern space.

Example:
sed -n 's/foo/bar/p' file.txt (Only prints lines where substitution occurred)

f file

Parse script from file

Example:
sed -f script.sed file.txt

r file

Read content from file, and append to current file

Example:
sed '/pattern/r file.txt' main.txt

Basic Commands

s/pattern/replacement/

Substitute ‘pattern’ with ‘replacement’.

Example:
sed 's/foo/bar/' file.txt (Replaces first occurrence of ‘foo’ with ‘bar’ in each line)

g flag

Global substitution. Replaces all occurrences in a line.

Example:
sed 's/foo/bar/g' file.txt (Replaces all ‘foo’ with ‘bar’ in each line)

i flag

Case-insensitive substitution.

Example:
sed 's/foo/bar/i' file.txt (Replaces ‘foo’ or ‘FOO’ with ‘bar’)

p

Print the current pattern space.

Example:
sed -n 's/foo/bar/p' file.txt (Print lines where substitution occurred)

d

Delete the current pattern space (line).

Example:
sed '/foo/d' file.txt (Delete lines containing ‘foo’)

Advanced Text Manipulation

Using Addresses

sed '2s/foo/bar/' file.txt

Substitute only on line 2.

sed '2,5s/foo/bar/' file.txt

Substitute on lines 2 through 5.

sed '2,$s/foo/bar/' file.txt

Substitute on line 2 to the end of the file.

sed '/start/,/end/s/foo/bar/' file.txt

Substitute between lines matching /start/ and /end/.

Delete Specific Lines

sed '1d' file.txt

Delete the first line.

sed '$d' file.txt

Delete the last line.

sed '1,3d' file.txt

Delete lines 1 to 3.

sed '/pattern/d' file.txt

Delete lines matching the pattern.

Insert and Append Text

sed '2i Text to insert' file.txt

Insert ‘Text to insert’ before line 2.

sed '2a Text to append' file.txt

Append ‘Text to append’ after line 2.

sed '/pattern/i Text' file.txt

Insert text before lines matching ‘pattern’.

sed '/pattern/a Text' file.txt

Append text after lines matching ‘pattern’.

Regular Expressions with Sed

Basic Regex

. (dot)

Matches any single character except newline.

Example:
sed 's/a.c/abc/' file.txt (Replaces ‘aac’, ‘abc’, etc., with ‘abc’)

* (asterisk)

Matches zero or more occurrences of the preceding character.

Example:
sed 's/ab*c/abc/' file.txt (Replaces ‘ac’, ‘abc’, ‘abbc’, etc., with ‘abc’)

[] (character class)

Matches any single character within the brackets.

Example:
sed 's/a[bc]d/aed/' file.txt (Replaces ‘abd’ or ‘acd’ with ‘aed’)

^ (caret)

Matches the beginning of a line (when inside brackets, negates the character class).

Example:
sed '/^foo/d' file.txt (Deletes lines starting with ‘foo’)

$ (dollar)

Matches the end of a line.

Example:
sed 's/foo$/bar/' file.txt (Replaces ‘foo’ at the end of the line with ‘bar’)

Extended Regex

+

Matches one or more occurrences of the preceding character (requires -E or -r).

Example:
sed -E 's/ab+c/abc/' file.txt

?

Matches zero or one occurrence of the preceding character (requires -E or -r).

Example:
sed -E 's/ab?c/abc/' file.txt

|

Specifies alternation (requires -E or -r).

Example:
sed -E 's/foo|bar/baz/' file.txt

()

Groups parts of the regex together (requires -E or -r).

Example:
sed -E 's/(ab)+c/abc/' file.txt

Character Classes

[[:alnum:]]

Alphanumeric characters.

[[:alpha:]]

Alphabetic characters.

[[:digit:]]

Digits (0-9).

[[:space:]]

Whitespace characters (space, tab, newline).

[[:upper:]]

Uppercase alphabetic characters.

[[:lower:]]

Lowercase alphabetic characters.

Practical Sed Examples

Numbering Lines

sed = file.txt | sed 'N;s/\n/ /'

Adds line numbers to each line.

Remove Blank Lines

sed '/^$/d' file.txt

Deletes all blank lines from file.txt.

Convert DOS to Unix Line Endings

sed 's/\r$//' file.txt

Removes carriage return characters at the end of lines.

Extract Data

Extract IP Address from interface config:

ifconfig eth0 | sed -n 's/.*inet //p' | sed -n 's/ netmask.*//p'

Working with variables

Using shell variables in sed:

text="Hello"
sed "s/$text/World/" file.txt