Sed cheat sheet
Summary
Cheat sheet for GNU sed
, the powerful Unix utility designed for non-interactive text manipulation and automation like substitution, deletion, and transformation. Contains many sed
command examples.
Introduction #
sed
is an indispensable tool for text processing and automation in Unix-like environments. Its concise syntax and powerful features enable users to perform complex editing tasks efficiently. Whether you are replacing text, deleting lines, or performing intricate transformations, sed
provides a versatile solution tailored to a wide range of scripting and command-line needs.
What is sed
? #
sed
, short for stream editor, is a powerful Unix utility used for parsing and transforming text in a pipeline. Unlike interactive text editors, sed
processes text non-interactively, making it ideal for automating repetitive editing tasks, especially in shell scripts.
How sed
Works #
sed
reads input line by line (streaming), applies specified editing commands to each line, and outputs the result. It can perform a variety of text manipulations, including substitution, deletion, insertion, and more, using a concise and expressive syntax.
Basic Workflow #
- Input:
sed
takes input from standard input (stdin) or from a file. - Processing:
sed
applies the editing commands to each line of the input. - Output: The transformed text is sent to standard output (stdout).
Basic Syntax #
sed [OPTIONS] 'SCRIPT' [INPUTFILE...]
- OPTIONS: Flags that modify
sed
behavior (e.g.,-e
,-f
,-i
). - SCRIPT: One or more editing commands enclosed in single quotes.
- INPUTFILE: Files to be processed. If omitted,
sed
reads from stdin.
Substitution (s
) #
Replace occurrences of a pattern with a replacement.
sed 's/pattern/replacement/' file.txt
Possible flags:
g
: Global replacement (all occurrences in a line).i
: Case-insensitive matching.number
: Replace the nth occurrence.
Replace first occurrence of “foo” with “bar” in each line #
sed 's/foo/bar/' file.txt
Replace all occurrences of “foo” with “bar” #
sed 's/foo/bar/g' file.txt
Change file name extensions #
sed 's/\.txt$/.md/' filenames.txt
Replace the second occurrence of “foo” with “bar” #
sed 's/foo/bar/2' file.txt
Replace “foo” with “bar” in lines 10 to 20 #
sed '10,20s/foo/bar/' file.txt # Substitute on lines 10 to 20
Replace “foo” with “bar” only in lines containing “baz” #
sed '/baz/s/foo/bar/' file.txt
Trim leading and trailing whitespace #
sed 's/^[ \t]*//;s/[ \t]*$//' file.txt
Replace tabs with spaces #
sed 's/\t/ /g' file.txt # 4 spaces in this case
Deletion (d
) #
Delete lines matching a pattern or at specific addresses.
sed '/pattern/d' file.txt
Remove blank lines #
sed '/^$/d' file.txt
Delete second line #
sed '2d' file.txt
Delete lines 3 to 5 #
sed '3,5d' file.txt
Delete lines containing “error” #
sed '/error/d' file.txt
Insertion (i
) and Appending (a
) #
Insert or append text before or after a line matching a pattern or at a specific address.
sed 'ADDRESS i\
Text to insert' file.txt
and
sed 'ADDRESS a\
Text to append' file.txt
Insert “Start of File” before the first line #
sed '1i\
Start of File' file.txt
Append “End of File” after the last line #
sed '$a\
End of File' file.txt
Insert line numbers #
sed = file.txt | sed 'N;s/\n/\t/'
Change (c
) #
Replace entire lines matching a pattern or address.
sed 'ADDRESS c\
New text' file.txt
Change lines containing “foo” to “bar”: #
sed '/foo/c\
bar' file.txt
Print (p
) #
Print specific lines. Often used with the -n
option to suppress automatic printing.
sed -n 'ADDRESS p' file.txt
Print only lines containing “success” #
sed -n '/success/p' file.txt
Addressing in sed #
Addresses in sed
determine which lines the commands apply to. These can be specific line numbers, patterns, or a combination of both. Below are examples showcasing different types of addressing.
Single line addressing #
Apply a command to a specific line by specifying its line number.
Delete the 3rd line of a file #
sed '3d' file.txt
Replace the content of the 5th line #
sed '5c\
Range Addressing #
Apply commands to a range of lines using the START,END
syntax.
Delete lines 2 to 4 #
sed '2,4d' file.txt
Replace the text “error” with “fixed” in lines 10 through 20 #
sed '10,20s/error/fixed/g' file.txt
Pattern Addressing #
Apply commands to lines matching a specific pattern.
Delete lines containing “DEBUG” #
sed '/DEBUG/d' file.txt
Add a prefix “LOG: “ to lines containing “ERROR” #
sed '/ERROR/s/^/LOG: /' file.txt
Range with Patterns #
Use patterns to define a range of lines.
Delete all lines between those matching “START” and “END” #
sed '/START/,/END/d' file.txt
Replace “foo” with “bar” in lines between “begin” and “stop” #
sed '/begin/,/stop/s/foo/bar/g' file.txt
Special Line Addressing #
Use special symbols like $
and ~
for specific addressing.
Delete the last line of a file #
sed '$d' file.txt
Replace “END” with “FINISH” in the last line #
sed '$s/END/FINISH/' file.txt
Delete every 4th line (GNU sed) #
sed '4~4d' file.txt
Combined Addressing #
Combine line numbers, ranges, and patterns for precise targeting.
Delete lines 1 to 5 and any line containing “TODO” #
sed '1,5d; /TODO/d' file.txt
Replace “abc” with “xyz” on the 2nd line and any line containing “keyword” #
sed '2s/abc/xyz/; /keyword/s/abc/xyz/' file.txt
Negating Patterns #
Use !
to negate an address, applying commands to lines not matching a condition.
Print all lines except those containing “SKIP” #
sed '/SKIP/!p' file.txt
Add a header to all lines except the first line #
sed '1!s/^/Header: /' file.txt
Table of addressing techniques #
Address Type | Syntax | Example Command |
---|---|---|
Single Line | N | sed '3d' file.txt |
Line Range | START,END | sed '5,10d' file.txt |
Pattern Match | /PATTERN/ | sed '/error/d' file.txt |
Range with Patterns | /START/,/END/ | sed '/begin/,/stop/d' file.txt |
Last Line | $ | sed '$s/END/FINISH/' file.txt |
Every nth Line | N~M (GNU sed ) | sed '2~3d' file.txt |
Negate Pattern | ! | sed '/pattern/!d' file.txt |
Multiple Commands #
There are three different ways to apply multiple sed
commands in a single execution:
Using -e
option #
Use the -e
option to replace “foo” with “bar” and delete lines containing “baz”:
sed -e 's/foo/bar/' -e '/baz/d' file.txt
Using a sed
script file with -f
#
Create a script file script.sed
to replace “foo” with “bar” and delete lines containing “baz”:
s/foo/bar/
bazd
Run sed
with the script:
sed -f script.sed file.txt
Using Semicolons within a single script #
Using a semicolon to replace “foo” with “bar” and delete lines containing “baz”:
sed 's/foo/bar/; /baz/d' file.txt
Regular Expressions #
Leverage powerful pattern matching for flexible text processing.
Basic Regex #
.
: Any single character.*
: Zero or more of the preceding element.^
: Start of line.$
: End of line.
Extended Regex #
Enabled with -E
or -r
flags.
+
: One or more.?
: Zero or one.|
: Alternation.()
and{}
: Grouping and quantifiers.
Further readings #
Sources and recommended, further resources on the topic:
License
License: Sed cheat sheet by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/publications/sed-cheat-sheet">Sed cheat sheet</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>
For more information see the DITig legal page.