Sed
lastmod 2026-02-06created 2014-12-1257 edits
The sed (stream editor) command combines part of the functionality of the ed command and the grep command. Like the grep command, sed is a filter, but it can modify files, while interactive processing like ed is not possible. The sed command reads input one line at a time and writes it to standard output. For each line it reads, sed performs a substitution in the format used in ed. If there is a matching string, it outputs the line with the string substituted; if there is no matching string, the line is output unmodified. The advantage of sed over ed is that it reads, modifies, and outputs lines one at a time, so it doesn't use a buffer in memory. Without a buffer, it can work on files of any size. Tools that use a buffer, like ed, can't process files larger than the buffer, and the buffer is usually about 1MB. So sed is mainly used for processing very large files. The way you invoke sed is the same as the grep command; the only difference is that it uses a full substitution operator. When using sed on the command line or in a shell script, you may need quoting and some options.
Command
-
a\: Appends one or more new lines after the current line. -
c\: Replaces the contents of the current line with new contents. -
i\: Inserts above the current line. -
d: Deletes the line. -
l: Prints non-printing special characters. -
p: Prints the line. -
n: Makes the next input line be processed by the next command rather than the first command. -
q: Quits sed. -
r: Reads lines from a file. -
!: Applies the command to all lines except the selected lines. -
s: Substitutes a string.
Address format
-
number: A number applies to a specific line number. -
first~step: Once every step lines, starting from the line the first address points to -
$: The last line -
/regexp/: Lines with a value matching the regular expression pattern -
\cregexpc: c can be any character; this also denotes a regular expression. -
0,addr2: Used to work around the problem in 1,addr2 where it isn't handled correctly when addr2 is a regular expression that matches line 1. -
addr1,+N: Selects addr1 plus N more lines. -
addr1,~N: Selects from addr1 up to the first line that is a multiple of N.
Options
-
-e: Interprets the following string as an editing command. 1 -
-f: Used to specify a sed script file. -
-n: Suppresses standard output (stdout) for each input (INPUT). -
-i: Edit files in-place. That is, sed's changes are written directly to the file.- Be careful when using this option, because implementations differ slightly by OS.2
- On Linux implementations
-ialone is enough, but on BSD-based systems you must use-i {extension}. - This option doesn't modify the file itself directly; it creates a temporary file. So what you specify is the extension of that temporary file.
In-place sed
The method compatible with both BSD-based and regular Linux-based systems is as follows.
Delete Examples
-
sed '1,5d' dummy.txt - Delete Line
-
sed '1d;2d;3d;$d' dummy.txt - Delete multiple lines
-
sed '2,4!d' dummy.txt - Delete everything except specific lines
-
sed '/^$/d' dummy.txt - Delete empty lines
-
sed '/the/d' dummy.txt - Delete lines matching the pattern
-
sed '/brown\|the/d' dummy.txt - Search for word patterns separated with the
\|symbol
-
sed '/the/{N;d;}' dummy.txt - If the pattern matches, delete the line and also delete the next line
-
sed '/fox/,$d' dummy.txt - Delete lines from the matched pattern to the end
TAG
To "tag" part of a pattern, surround it with \(</code> on the left and <code>\) on the right. Then in the substitution pattern you can refer to the first tag as \1, the second as \2, and so on. An example follows.
INSERT NEW LINE
When using s///g, you can add a Newline as follows.
Detail comment
This works in bash, tested on Linux and OS X:
In general, for $ followed by a string literal in single quotes bash performs C-style backslash substitution, e.g. $'\t' is translated to a literal tab. Plus, sed wants your newline literal to be escaped with a backslash, hence the \ before $. And finally, the dollar sign itself shouldn't be quoted so that it's interpreted by the shell, therefore we close the quote before the $ and then open it again.
How to combine multiple lines of output
You can use the following tools:
- bash - Concatenate lines given by grep -A - Stack Overflow
- sed#How to combine multiple lines of output
- awk#How to combine multiple lines of output
- tr#How to combine multiple lines of output
| TODO |
| Needs testing |
There are oh so many ways to do it, here is one with sed:
Explanation:
-
-nmeans "don't print the output automatically" -
/^LABEL/: search for label -
{ ... }: command block -
N; N;take two lines, append them to the pattern space -
s/\n/\t/g: substitute the new lines by tabs in the pattern space -
p: print out the pattern space -
filenamename of your file.
Tip
- In shell scripts, use single quotes (
') rather than double quotes (") for quoting whenever possible to avoid errors. - GNU versions of sed and awk support
+, but it must be escaped. 34
Example
-
sed 's#3#99#g' datafile - The character right after the
scommand is the delimiter that separates the search string and the replacement string. By default/is used as the delimiter, but you can change it to another character. This is useful when searching for strings that contain/itself. - Personally, it's best to use a character that can't appear in file paths (such as
;).
-
sed -e 's/:<:Rectangle:>:/Rect/g' - Changes the "word" Rectangle to Rect.
-
sed -e '0,/claudio/ s/claudio/claudia/' nomi - Converts claudio to claudia between line 0 and the first occurrence of the pattern claudio. That is, convert claudio to claudia only once.
-
sed '/^[ ]*$/d' - Delete lines that are empty or consist only of spaces.
-
sed '1,3p' [파일] - Prints lines 1 through 3 of the file.
-
sed '1,5!p' [파일] - Print the file except lines 1 through 5.
-
sed -n 42p [파일] - Prints line 42 of the file. Use the
-nflag to prevent duplicate output.
-
sed "/^$/d" inputstring - Delete all blank lines.
-
sed "/^ *$/d" inputstring - Delete all blank lines consisting of spaces.
-
sed "8d" inputstring - Delete the 8th line.
-
sed "1,/^$/d" inputstring - Delete from the first line up to the first empty line.
-
sed -n '100,$p' - Print from line 100 to the end.
-
sed -n '/BEGIN/$,p'sed -n '/BEGIN/,$p' - Print from the moment the
BEGINstring appears to the end ($).
-
sed -n '1,/END/p' - Print from the first line up to the moment the
ENDstring appears.
-
sed -n '/BEGIN/,/END/p' - Print everything between
BEGINandEND.
-
sed -n "/Pattern1/p" inputstring - Print lines containing Pattern1. (The -n option is required)
-
sed "s/Pattern1/Pattern2/" inputstring - Substitute the first "Pattern1" on each line of input with "Pattern2".
-
sed "s/Pattern1/Pattern2/g" inputstring - Substitute every occurrence of "Pattern1" on each line of input with "Pattern2".
-
sed "s/ *$//" inputstring - Delete trailing spaces at the end of every line.
-
sed "s/00*/0/g" inputstring - Squeeze (convert) all consecutive 0s into a single 0.
-
sed "/GUI/d" inputstring - Delete all lines containing "Pattern1".
-
sed "s/GUI//g" inputstring - Remove "Pattern1" from lines where "Pattern1" appears.
-
sed 's/ */ /' list.txt - Replaces one or more spaces with a single space
-
sed ‘/^love /a\ -----’ datafile - Appends the string after the
a\command after lines that start with the patternlovefollowed by a space.
-
sed ‘/love/i\ -----’ datafile - After finding the pattern
love, inserts the text following thei\command above the line containinglove.
-
sed ‘5q’ datafile - Prints up to line 5 and then quits the sed program.
-
sed ‘/love/{ s/love/peace/;q; }’ datafile - On the line where the pattern
loveis found,loveis replaced withpeaceand sed quits with theqcommand.
-
sed -n '10p' file.txt - Print only 1 line
-
sed '5!/s/foo/bar/' file.txt - Substitute text on all lines except line 5
-
sed '/^hello/ s/h/H/' file.txt - Regex substitution on all lines starting with a specific string
-
sed '5,$ s/foo/bar/' file.txt - Substitute only from line 5 to the last line
-
sed '/^$/d' file - Delete empty files
-
sed -nE '/^foo/,/^bar/p' file.txt - Print all lines between two regular expressions
-
sed 's_/bin/bash_/bin/sh_' file.txt - Use a custom delimiter to easily find strings containing slashes
-
sed -E 's_([a-zA-Z0-9_]+) \1_\1_ig' f1 - Remove duplicate strings separated by a space (excluding ones repeated three times)
-
sed 's_foo_bar_w replaced.txt' file.txt - Find and replace a specific pattern, then save only the modified lines to another file
-
sed -e 's_foo_bar_' -e 's_hello_HELLO_' file.txt - Multiple substitutions at once
-
sed -E '/start/,/end/ s/#.*//' file.txt - Remove all comments between two keywords and leave empty lines
-
sed -E '/^#/d' f1
:Delete all comments starting with #
-
sed '/start/q' file.txt - Print only until a specific pattern appears
-
sed '5 r newfile.txt' file.txt - Insert a file after a specific line
-
sed '/foo/a\AFTER FOO' file.txt - Append text on the line after a specific regex match
-
sed '/foo/c\FOO IS CHANGED' file.txt - Replace the line matching a specific regex
-
sed -E '/^#/w comments.txt' file.txt
:Save all comments starting with # to another file
-
sed -n '1~2p' file.txt - Print only every second line (every other line; can be changed to 3/4 and so on)
-
sed -i.bak 's/hello/HELLO/' file.txt - Edit the file directly but create a backup
-
sed -E '/^#/G G' file.txt - Add two empty lines after a regex match
Text Processing Commands
|
See also
Favorite site
- [Recommended] How to use SED 5
- [Recommended] regexper (diagrams)
- How to use the Sed (streamlined editor) command
- How to use the SED command 6
- Linux Portal: Sed (streamlined editor)
- KLDP: Advanced Bash-Scripting Guide: A complete guide to shell scripting with Bash // Text processing commands 7
- KLDP: Advanced Bash-Scripting Guide: Appendix B.1. Sed
- Wikipedia (en) description of SED
- How to use SED and SWK
- SED stream editor
- How to use sed and awk
- [Recommended] Removing newline characters with sed (regular expressions)
Tutorials
- [Recommended] Sed stream editor guide - mug896 8 (archive)
References
-
For reference, sed, awk, and grep all use the
-eoption to specify that the following string is a command or a set of commands. If the string is just a single command, you can omit this option. ↩ -
https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html ↩
-
https://wiki.kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/x10468.html ↩
-
How_to_use_SED_-_Wireframe.pdf ↩
-
Stone.backrush.com-how_to_use_sed.zip ↩
-
Adv-bash-scr-howto.zip ↩
-
Mug896-sed-stream-editor-master-f59f136.zip (2020-10-07) ↩