Skip to content
Search

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 -i alone 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

$ find . -name "*.cpp" -o -name "*.hpp" | xargs sed -i .tmp "s/oldstr/newstr/g"

The method compatible with both BSD-based and regular Linux-based systems is as follows.

$ sed -i.tmp "s/oldstr/newstr/g" temp.txt

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.

sed 's/\(Pat\)\([^a-z]\)/\1ricia\2/g' ljs

INSERT NEW LINE

When using s///g, you can add a Newline as follows.

sed -e 's/\(libtbag\/config\.h\>\)/\1\'$'\n#include <libtbag\/predef\.hpp>/g' unit.hpp

Detail comment

This works in bash, tested on Linux and OS X:

sed 's/regexp/\'$'\n/g'

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:

TODO

Needs testing

There are oh so many ways to do it, here is one with sed:

sed -n "/^LABEL/ { N; N; s/\n/\t/g ; p }" filename

Explanation:

  • -n means "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
  • filename name 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 s command 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 -n flag 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 BEGIN string appears to the end ($).
sed -n '1,/END/p'
Print from the first line up to the moment the END string appears.
sed -n '/BEGIN/,/END/p'
Print everything between BEGIN and END.
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 pattern love followed by a space.
sed ‘/love/i\ -----’ datafile
After finding the pattern love, inserts the text following the i\ command above the line containing love.
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 love is found, love is replaced with peace and sed quits with the q command.
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

List of text processing commands

Text

Common

grep, sed, awk, cat, xargs, rev, tee, ack, tr, stdbuf, split

Diff

diff, patch

Cutting

cut, head, tail, less, more, split, watch

Sort

uniq, sort

Hash

base64, md5sum, sha1sum, sha224sum, sha256sum, sha384sum, sha512sum

Clipboard

clip (Windows), pbcopy (Mac OSX), pbpaste (Mac OSX), xclip (Debian)

E.T.C

regexp, parallel

See also

Favorite site

Tutorials

References


  1. For reference, sed, awk, and grep all use the -e option 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. ↩

  2. Tested on Linux and FreeBSD (strictly speaking, Mac OSX). ↩

  3. https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html ↩

  4. https://wiki.kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/x10468.html ↩

  5. How_to_use_SED_-_Wireframe.pdf ↩

  6. Stone.backrush.com-how_to_use_sed.zip ↩

  7. Adv-bash-scr-howto.zip ↩

  8. Mug896-sed-stream-editor-master-f59f136.zip (2020-10-07) ↩

author Zer0 sitemap