community man page

sed

Edit text in a scriptable manner. See also: `awk`, `ed`.

11 examples·1 recipe·source: tldr · CC-BY·status: verified
Reading is open to everyone. Suggesting a flag, fixing an example or adding a recipe is opening to verified-email accounts soon — every change is moderated before it goes live.
curl https://curlhub.sh/man/sed
same page, in your terminal

Examples

substitute all occurrences of "apple" with "mango" on all lines, print to `stdout` GNU

command | sed 's/apple/mango/g'

Replace "apple" with "mango" in-place in a file (overwriting original file) GNU

sed [-i|--in-place] 's/apple/mango/g' path/to/file

Run multiple substitutions in one command GNU

command | sed -e 's/apple/mango/g' -e 's/orange/lime/g'

Use a custom delimiter (useful when the pattern contains slashes) GNU

command | sed 's#////#____#g'

delete lines 1 to 5 of a file and back up the original file with a `.orig` extension GNU

sed [-i|--in-place=].orig '1,5d' path/to/file

print only the first line to `stdout` GNU

command | sed [-n|--quiet] '1p'

insert a new line at the beginning of a file, overwriting the original file GNU

sed [-i|--in-place] '1i\your new line text\' path/to/file

Delete blank lines (with or without spaces/tabs) from a file, overwriting the original file GNU

sed [-i|--in-place] '/^[[:space:]]*$/d' path/to/file

Replace all `apple` (basic `regex`) occurrences with `mango` (basic `regex`) in all input lines and print the result to `stdout`

command | sed 's/apple/mango/g'

Execute a specific script file and print the result to `stdout`

command | sed -f path/to/script.sed

Print just a first line to `stdout`

command | sed -n '1p'

Recipes

⚠ destructiveadvancedGNU

sed -i.bak 's/localhost/127.0.0.1/g' config.ini

Replace every occurrence of localhost with 127.0.0.1 in config.ini, editing the file in place. `-i.bak` writes the edit in place but first saves the original as config.ini.bak. DESTRUCTIVE: `-i` rewrites the file; the `.bak` suffix is your undo path (plain `-i` with no suffix leaves no backup).