Kolam Ayer MakersLinux Foundations

sed

Use

printf 'hello makers\n' | sed 's/makers/world/'

What It Does

sed transforms text as it streams through editing rules. Most beginner use is substitution.

Simple Substitutions

Replace the first match on each line:

printf 'hello makers\n' | sed 's/makers/world/'

Replace every match on each line:

printf 'ha ha ha\n' | sed 's/ha/HA/g'

Use a different delimiter when the pattern contains /:

printf 'https://example.org\n' | sed 's#https://#http://#'

Delete blank lines:

sed '/^$/d' notes.txt

Print only lines 1 through 5:

sed -n '1,5p' notes.txt

Capture Example

printf '# heading\n' | sed 's/^# \(.*\)$/<h1>\1<\/h1>/'

In-Place Editing

Use in-place editing only after testing without it:

cp page.html page.html.backup
sed 's/old/new/' page.html
sed -i 's/old/new/' page.html

sed -i changes the file. Test on stdout first.

Watch Out

Docs Pointers