sed

Replace a word in a file or in a pipe, every time it appears, or right inside the file with sed -i.

>_ Try it: a simulated Linux, right here
Loading the terminal…

Examples

  • sed "s/3000/8080/" app.conf
  • sed "s/example/dockerlinux/g" links.txt
  • sed "s|http://|https://|" links.txt
  • sed -i "s/debug=true/debug=false/" app.conf
  • cat app.conf
  • echo "hello world" | sed "s/world/Linux/"

How it works

sed "s/OLD/NEW/" FILE prints the file with the first OLD on each line replaced by NEW. The file itself is not changed — you see the result.

Add g at the end to replace every match on a line, and -i to write the change into the file (in place). Check the output first, then run it again with -i.

Syntax

sed [-i] "s/OLD/NEW/[g]" [FILE]...

Options

OptionWhat it does
s/OLD/NEW/replace the first OLD on each line with NEW
gglobal: every match on the line, not just the first
-iin place: save the change into the file
s|A|B|any character can separate the parts: handy when the text has slashes

Good to know

  • Without -i sed never touches the file: try your replacement safely first, then add -i.
  • OLD is a pattern: a dot means "any character". To replace a literal dot, write \..