sed, a stream editor

Table of Contents

Running sed

input.txt
hello
sed 's/hello/world/' input.txt > output.txt
cat output.txt
world

Following lines are equivalent to above:

sed 's/hello/world/' input.txt > output.txt
sed 's/hello/world/' < input.txt > output.txt
cat input.txt | sed 's/hello/world/' - > output.txt
sed -i 's/hello/world/' file.txt
sed -n '45p' file.txt
-i
in-place
-n
suppress all lines
'45p'
print line45

Following lines are all equivalent:

sed 's/hello/world/' input.txt > output.txt

sed -e 's/hello/world/' input.txt > output.txt
sed --expression='s/hello/world/' input.txt > output.txt

echo 's/hello/world/' > myscript.sed
sed -f myscript.sed input.txt > output.txt
sed --file=myscript.sed input.txt > output.txt

Command-Line Options

See /sed/cli

sed scripts

<e sed commands follow this syntax:

[addr]X[options]
sed '30,35d' input.txt > output.txt
sed '/^foo/q42' input.txt > output.txt

See /sed/commands

Addresses: selecting lines