_                    _
  ___ | |__   __ _ _______ | |_
 / _ \| '_ \ / _` |_  / _ \| __|
| (_) | | | | (_| |/ / (_) | |_
 \___/|_| |_|\__,_/___\___/ \__|

sedusage and tips

replace old with new : s/old/new/
replace all occurrences : s/old/new/g
replace the second occurrence : s/l/p/2

$ echo "hello" | sed "s/l/p/"
heplo
$ echo "hello" | sed "s/l/p/g"
heppo
$ echo "hello" | sed "s/l/p/2"
helpo

The typical separator is /, which can conflict with pathnames. An option is to use another character as a separtor, eg.: @ :

echo "/some/path" | sed "s/\/some\/path/\/other\/directory/"
echo "/some/path" | sed "s@/some/path@/other/directory@"

back to top

The previous examples can be applied to files:

$ cat file.txt
hello
$ sed "s/l/p/" file.txt
heplo
$ cat file.txt
hello
The previous example displays the substitution, to apply the change to the file, use -i:
$ cat file.txt
hello
$ sed -i "s/l/p/" file.txt
$ cat file.txt
heplo

back to top

text_manipulation

- OpenBSD manpages: sed(1)

back to top

ohazot | about | ohazot.com <admin@ohazot.com>

This document applies to: OpenBSD 7.7 linux| Created:2025-10-03|Updated:2026-03-25|