sed Commands

Complete sed commands reference — 38 commands across 2 categories. Quick reference cheat sheet for Linux, macOS & BSD.

sed reads input a line at a time into a pattern space, applies a script of commands to it, and prints the result — and almost every practical use is one command, s, with two or three options in front. The table lists GNU sed's options and script commands from its manual; the notes cover the handful that account for nearly all real invocations and the mistakes that follow from them.

Options (15)

ShortcutAction
sed -nsuppress automatic printing of pattern space
sed --debugannotate program execution
sed -e scriptadd the script to the commands to be executed
sed -f script-fileadd the contents of script-file to the commands to be executed
sed --follow-symlinksfollow symlinks when processing in place
sed -i[SUFFIX]edit files in place (makes backup if SUFFIX supplied)
sed -l Nspecify the desired line-wrap length for the 'l' command
sed --posixdisable all GNU extensions
sed -Euse extended regular expressions in the script (for portability use POSIX -E)
sed -sconsider files as separate rather than as a single, continuous long stream
sed --sandboxoperate in sandbox mode (disable e/r/w commands)
sed -uload minimal amounts of data from the input files and flush the output buffers more often
sed -zseparate lines by NUL characters
sed --helpdisplay this help and exit
sed --versionoutput version information and exit

sed Script Commands (23)

ShortcutAction
: labelLabel for b and t commands
#commentThe comment extends until the next newline (or the end of a -e script fragment)
textAppend text, which has each embedded newline preceded by a backslash
q [exit-code]Immediately quit the sed script without processing any more input, except that if auto-pri
Q [exit-code]Immediately quit the sed script without processing any more input
r filenameAppend text read from filename
R filenameAppend a line read from filename
b labelBranch to label; if label is omitted, branch to end of script
DIf pattern space contains no newline, start a normal new cycle as if the d command was iss
l widthList out the current line in a "visually unambiguous" form, breaking it at width character
PPrint up to the first embedded newline of the current pattern space
s/regexp/replacement/Attempt to match regexp against the pattern space
t labelIf a s/// has done a successful substitution since the last input line was read and since
T labelIf no s/// has done a successful substitution since the last input line was read and since
w filenameWrite the current pattern space to filename
W filenameWrite the first line of the current pattern space to filename
y/source/dest/Transliterate the characters in the pattern space which appear in source to the correspond
first~stepMatch every step'th line starting with line first
/regexp/Match lines matching the regular expression regexp
\cregexpcMatch lines matching the regular expression regexp
0,addr2Start out in "matched first address" state, until addr2 is found
addr1,+NWill match addr1 and the N lines following addr1
addr1,~NWill match addr1 and the lines following addr1 until the next line whose input line number
📄 View Printable Cheat Sheet — Download as PDF or print · 🧩 Combine with other tools

Related Shortcut Pages

awk Regex (Regular Expressions) Bash / Shell Vim tmux GNU Screen

Search 18,500+ shortcuts across 268 platforms

Explore All Platforms Practice Shortcuts
📜 Every shortcut on this page is taken from the official sed documentation — man7.org
🔧 Spotted an error or a missing shortcut? Suggest an edit on GitHub — every accepted fix goes live on this page, the API and the CLI.

The substitution and its flags

s/regexp/replacement/ replaces the first match on each line; a trailing g replaces every match, I ignores case, and a number replaces only that occurrence. Any delimiter works — s|/usr/local|/opt| avoids escaping slashes in paths. sed -E switches to extended regular expressions so groups are (...) rather than \(...\), and \1 in the replacement refers back to a group. sed -n suppresses the default print so only lines you explicitly print with a p flag appear, which turns sed into a grep with capture groups: sed -nE 's/^user=(.*)/\1/p'.

Editing files in place

sed -i[SUFFIX] edits the file rather than printing; -i.bak keeps a backup and plain -i does not. On macOS the BSD sed requires an argument, so -i '' is the portable form. Combine with sed -s to treat several files separately when addresses such as 1 or $ should apply per file, and sed --sandbox when running scripts you did not write, since it disables the commands that execute or write arbitrary files.

Addresses and the rest of the script

A command can be limited to lines by address: /regexp/ for matching lines, a number for one line, 1,10 for a range, first~step for every n-th line and addr1,+N for a line and the N after it. Deleting matching lines is /pattern/d; printing a range is -n '10,20p'. y/source/dest/ transliterates characters, q [exit-code] stops after the current line — sed 10q is a fast head — and sed -e script or sed -f script-file chain several commands. The branching commands b label and t label exist for multi-line scripts, though anything needing them is usually clearer in awk.

Frequently asked questions

How do I replace all occurrences on a line?

Add the g flag: sed 's/old/new/g'. Without it only the first match per line changes.

How do I edit a file in place safely?

sed -i.bak 's/old/new/' file keeps a backup as file.bak. On macOS write -i '' when you do not want a backup.

How do I print only the lines that match?

Use -n with the p flag: sed -n '/pattern/p' file, or -nE with a capture group to print just part of the line.

How do I delete lines matching a pattern?

sed '/pattern/d' file. Add -i to change the file itself.

🤖 Ask AI about sed shortcuts

Open your assistant with this page preloaded as the source — great for follow-up questions like "which of these work in other apps?"

ChatGPT Claude Perplexity Gemini Grok