Skip to content
Justice Tech School

sed Edits Text

sed edits streams. s/old/new/ substitutes, g makes it global, -n with p prints chosen lines, d deletes and -i edits files in place.

Example
printf "one two\none one\nthree\n" | sed "s/one/1/"
printf "one two\none one\nthree\n" | sed "s/one/1/g"
printf "a\nb\nc\nd\n" | sed -n "2,3p"
printf "a\nb\nc\n" | sed "/b/d"

Result

1 two
1 one
three
1 two
1 1
three
b
c
a
c

Exercise

Replace every match.

sed "s/a/b/"

Quiz Test

What does sed s/x/y/ do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.