Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. sed

sed

sed (Stream EDitor) is a command that processes text line by line. It performs editing operations such as substitution, deletion, insertion, and extraction without opening a file. It is commonly used in scripts and automated workflows.

Syntax

sed [options] 'command' [file...]
sed [options] -e 'command1' -e 'command2' [file...]

Commands and Options

Command / OptionDescription
s/before/after/Replaces the first match on each line.
s/before/after/gReplaces all matches on each line (global).
s/before/after/iReplaces matches case-insensitively.
-i fileEdits the file in place (directly).
-i.bak fileSaves a backup to .bak before editing in place.
-nSuppresses default output (used together with the p command).
/pattern/dDeletes lines matching the pattern.
/pattern/pPrints lines matching the pattern (used together with -n).
NdDeletes line N.
N,MdDeletes lines N through M.
/pattern/a textAppends text after each matching line.
/pattern/i textInserts text before each matching line.

Sample Code

The following files are used as examples.

data.txt
Hello World foo foo
This is a foo test
Another line here
config.txt
# Database settings
host=localhost
# Port number
port=3306

s/before/after/ replaces the first match on each line. Adding g replaces all matches.

sed 's/foo/bar/' data.txt
Hello World bar foo
This is a bar test
Another line here
sed 's/foo/bar/g' data.txt
Hello World bar bar
This is a bar test
Another line here

/pattern/d deletes lines matching the pattern. The example below removes comment lines (lines starting with #).

sed '/^#/d' config.txt
host=localhost
port=3306

/^$/d removes blank lines.

printf "line1\n\nline2\n\nline3\n" | sed '/^$/d'
line1
line2
line3

Combining -n and the p command extracts only specific lines.

sed -n '2,4p' config.txt
host=localhost
# Port number
port=3306

-e runs multiple substitutions at once.

sed -e 's/foo/bar/g' -e 's/World/Earth/' data.txt
Hello Earth bar bar
This is a bar test
Another line here

-i edits the file directly (in-place substitution). -i.bak creates a backup before editing.

sed -i 's/old/new/g' file.txt
sed -i.bak 's/old/new/g' file.txt

Removes leading and trailing whitespace (trim).

echo "  hello world  " | sed 's/^[[:space:]]*//; s/[[:space:]]*$//'
hello world

Deletes the first line (useful for skipping CSV headers, for example).

echo -e "name,age\nalice,30\nbob,25" | sed '1d'
alice,30
bob,25

Notes

When using sed -i for in-place editing, it is strongly recommended to create a backup first or work within a git-managed repository. On macOS, BSD sed is used instead of GNU sed, so some option syntax differs (for example, macOS requires an empty string: sed -i "").

For more complex text processing, use awk. For simple searches, see also grep.

If you find any errors or copyright issues, please .