sed (stream editor) processes text line by line, making it essential for substitution, deletion, and extraction in shell scripts and automation pipelines. It is pre-installed on virtually every Linux and macOS system and is one of the most versatile text-processing tools available.
LinuxsedCLIText Processingsed 's/old/new/' filename.txt
s command substitutes the first occurrence of "old" with "new" on each line. Output goes to stdout; the original file is not modified. This is the most fundamental sed operation.sed 's/old/new/g' filename.txt
g flag replaces every occurrence on each line, not just the first. This is the most common variant for bulk text replacement tasks like updating hostnames or config values across an entire file.sed -i 's/old/new/g' filename.txt
-i flag modifies the file in-place, overwriting the original. Always test without -i first to confirm the command produces the expected output before making permanent changes.sed -i.bak 's/old/new/g' filename.txt
-i creates a backup file (e.g., filename.txt.bak) before editing. This is the safest way to do in-place editing — the original is preserved if something goes wrong.sed '/^#/d' filename.txt
d command deletes all lines matching the pattern. This example removes comment lines starting with #. A common use case is stripping comments from config files before processing them with other tools.sed '/^$/d' filename.txt
^$ matches lines containing nothing between start and end anchors. This removes all empty lines from output, useful for cleaning up configuration files or condensing log output.sed -n '10p' filename.txt
-n flag suppresses automatic printing; p explicitly prints the matched line. Together they print only line 10 — much faster than opening a large file in an editor to check a single line.sed -n '10,20p' filename.txt
sed -e 's/foo/bar/g' -e 's/baz/qux/g' filename.txt
-e to chain multiple expressions in a single invocation. Each expression is applied sequentially to every line, making multi-pattern replacements efficient without reading the file multiple times.sed '/pattern/a\New line to insert' filename.txt
a command appends a new line after every line matching the pattern. Use i instead to insert before the match. Handy for automatically adding entries to config files in deployment scripts.sed 's/pattern/replacement/g' file for a basic substitution preview.-i flag.-i.bak instead of plain -i when editing important files.-e expressions for complex transformations in one pass.sed is a stream editor designed for simple line-by-line transformations like substitution and deletion. awk is a full scripting language for structured, field-based data processing. Use sed for quick text replacements and awk when you need to work with specific columns or perform calculations.
No — sed writes to standard output and leaves the original file untouched. You need the -i flag for in-place editing. Always test without -i first to verify the output is correct before making permanent changes.
Yes, macOS ships with BSD sed. The key difference from GNU sed: -i requires a suffix on macOS — use -i '' (empty string) for no backup. For full GNU compatibility, install it via Homebrew: brew install gnu-sed.