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
Command | Description |
---|---|
s/old/new/ | Substitute the first occurrence of old with new on each line |
s/old/new/g | Substitute all occurrences of old with new on each line |
p | Print the current line |
d | Delete the current line |
q | Quit processing the input |
Addressing
Command | Description |
---|---|
n | Move to the next line of input |
N | Append the next line of input to the current line |
1,5p | Print lines 1 through 5 |
/pattern/p | Print lines that match pattern |
! /pattern/p | Print lines that do not match pattern |
10,/pattern/p | Print lines 10 through the first line that matches pattern |
/pattern/,/pattern2/p | Print lines between the first line that matches pattern and the first line that matches pattern2 |
Modifiers
Command | Description |
---|---|
i | Ignore case when matching |
g | Replace all occurrences on each line |
p | Print the current line after substitution |
w file | Write the current line to file |
a\ text | Append text after the current line |
i\ text | Insert text before the current line |
c\ text | Replace the current line with text |
Special Characters
Character | Description |
---|---|
. | 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 |
Examples
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