Web Analytics

sed (Stream Editor)

Advanced~30 min read

The stream editor sed excels at quick, scriptable edits across streams and files.

Core Edits

Output
Click Run to execute your code
Note: Use single quotes around sed scripts to avoid shell interpolation.
Pro Tip: Combine multiple commands with -e or use a script file for complex edits.
Caution: On macOS, sed -i requires a backup suffix: -i ''.

Common Mistakes

1) Forgetting -n with p

# Wrong
sed '2,4p' file
# Correct (suppress default print)
sed -n '2,4p' file

2) Greedy dot

# Over-matching
sed -E 's/<.*>/X/'
# Safer
sed -E 's/<[^>]*>/X/g'

Exercise: Delete Comments

Task: From stdin, remove lines that are blank or start with #.

Output
Click Run to execute your code
Show Solution
sed -E '/^\s*($|#)/d'

Summary

  • Substitute with s///.
  • Delete with d, insert with i.
  • Use ranges and regex addresses for precision.

What's Next?

Process structured text with awk.