UnixDroid

Text · Linux command

sed command in Linux

What does sed do?

sed is a stream editor: most often used to find and replace text, e.g. sed 's/old/new/g' file.

sed [-i] 's/find/replace/g' [file]

Most useful options

OptionWhat it does
s/a/b/replace the first a with b on each line
greplace all on the line
-iedit the file in place
-n 5pprint only line 5

sed examples

$ sed 's/http/https/g' urls.txt Try it ›

replace everywhere

$ sed -n '10,20p' file Try it ›

print lines 10-20

$ sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config Try it ›

harden SSH

In security work

Admins harden config files with sed in scripts; always back up before sed -i.

Questions about sed

How do I replace text in a file in Linux?

sed -i 's/old/new/g' file