Bash Tutorial · Pipes and Text Processing · Lesson 10 of 30
Bash sed: Edit Text Streams
Substitute, delete and print lines with sed, and edit files in place safely.
Substitution
sed 's/old/new/' replaces the first match on each line; add g for all matches. Any character can be the
delimiter, which helps with paths: s|/usr/local|/opt|.
echo "Hello World, hello world" | sed 's/hello/Hi/'
echo "Hello World, hello world" | sed 's/hello/Hi/gI'
echo "/usr/local/bin/app" | sed 's|/usr/local|/opt|'
echo "2026-01-15" | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3.\2.\1/'
Output
Hello World, Hi world Hi World, Hi world /opt/bin/app 15.01.2026
Run this on your computer — see “Get Started” for setup.
Selecting lines
sed -n '3p'prints line 3;sed -n '/ERROR/p'prints matching lines.sed '/^#/d'deletes comment lines;sed '1d'deletes the first line.
Editing files in place
sed -i 's/a/b/' file changes the file itself (GNU sed). On macOS use sed -i '' .... Keep a backup with
-i.bak when in doubt.
cat > app.conf <<'EOF'
# database settings
db_host=localhost
db_port=5432
# debug
debug=true
EOF
sed -i.bak 's/^debug=true/debug=false/' app.conf
sed '/^#/d' app.conf
ls app.conf*
Output
db_host=localhost db_port=5432 debug=false app.conf app.conf.bak
Run this on your computer — see “Get Started” for setup.
Test yourself
What does the g flag in s/a/b/g do?
Makes the match case-insensitiveReplaces every match on the line, not just the firstEdits the file in placePrints only changed lines
Show answer
Answer: Replaces every match on the line, not just the first