Getting Started

Examples

$ sed 's/old/new/g' file.txt
$ sed 's/old/new/g' file.txt > new.txt
$ sed 's/old/new/g' -i file.txt
$ sed 's/old/new/g' -i.backup file.txt

See: Sed examples

Sed script

$ echo 's/h/H/g' >> hello.sed
$ echo 's/w/W/g' >> hello.sed
$ echo "hello world" | sed -f hello.sed
Hello World

Use -f to execute sed script file

Multiple commands

$ echo "hello world" | sed -e 's/h/H/g' -e 's/w/W/g'
Hello World

Use -e to execute multiple sed commands

Option Examples

Option Example Description
-i sed -ibak 's/On/Off/' php.ini Backup and modify input file directly
-E sed -E 's/[0-9]+//g' input-file Use extended regular expressions
-n sed -n '3 p' config.conf Suppress default pattern space printing
-f sed -f script.sed config.conf Execute sed script file
-e sed -e 'command1' -e 'command2' input-file Execute multiple sed commands

Sed Usage

Syntax

$ sed [options] command [input-file]

With pipeline

$ cat report.txt | sed 's/Nick/John/g'
$ echo '123abc' | sed 's/[0-9]+//g'
Comments