Sed Cheat Sheet

In Linux, Sed, Unix

Sed, short for Stream Editor, is a powerful command-line tool used for text manipulation in Unix and Linux systems. It is a non-interactive editor that reads text from a file or standard input, performs editing operations on the text, and then outputs the modified text to standard output.

Sed is a versatile tool that can perform a wide range of text editing tasks, including search and replace, text filtering, and text transformation. It uses regular expressions to match patterns in the input text and then applies editing commands to modify the text.

One of the most common uses of sed is to perform search and replace operations on text files. For example, you can use sed to replace all occurrences of a word or phrase in a file with another word or phrase. Sed can also be used to delete lines or ranges of lines from a file, insert or append text to a file, and perform complex text transformations.

Sed is a powerful tool for automating text processing tasks and can be used in conjunction with other Unix tools like grep, awk, and cut to create powerful text processing pipelines. It is also highly customizable, with a wide range of options and command-line arguments that allow you to tailor its behavior to your specific needs.

This cheat sheet provides a quick reference guide to some of the most commonly used sed commands.

Basic Commands

s/old/new/Substitute the first occurrence of old with new on each line
s/old/new/gSubstitute all occurrences of old with new on each line
pPrint the current line
dDelete the current line
qQuit processing the input


nMove to the next line of input
NAppend the next line of input to the current line
1,5pPrint lines 1 through 5
/pattern/pPrint lines that match pattern
! /pattern/pPrint lines that do not match pattern
10,/pattern/pPrint lines 10 through the first line that matches pattern
/pattern/,/pattern2/pPrint lines between the first line that matches pattern and the first line that matches pattern2


iIgnore case when matching
gReplace all occurrences on each line
pPrint the current line after substitution
w fileWrite the current line to file
Append text after the current line
Insert text before the current line
Replace the current line with text

Special Characters

.Match any character
^Match the beginning of a line
$Match the end of a line
*Match zero or more occurrences
+Match one or more occurrences
?Match zero or one occurrence
[]Match any character within the brackets
[^]Match any character not within the brackets
\Escape special characters


Replace all occurrences of a word in a file

sed 's/old/new/g' file.txt

Print lines that match a pattern

sed -n '/pattern/p' file.txt

Delete lines that match a pattern

sed '/pattern/d' file.txt

Append text to the end of each line

sed 's/$/text/' file.txt

Write lines that match a pattern to a file

sed -n '/pattern/w output.txt' file.txt