Linux sed Command Examples

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 Processing
Basic Substitution
Replace first occurrence on each line
sed 's/old/new/' filename.txt
The 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.
Replace all occurrences globally
sed 's/old/new/g' filename.txt
The 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.
Edit file in-place
sed -i 's/old/new/g' filename.txt
The -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.
Edit in-place with backup
sed -i.bak 's/old/new/g' filename.txt
Appending a suffix to -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.
Line Operations
Delete lines matching a pattern
sed '/^#/d' filename.txt
The 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.
Delete blank lines
sed '/^$/d' filename.txt
The pattern ^$ 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.
Print a specific line number
sed -n '10p' filename.txt
The -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.
Print a range of lines
sed -n '10,20p' filename.txt
Prints lines 10 through 20 inclusive. A quick way to extract a specific section from a large file, such as a block of code or a specific log time window, without opening the whole file.
Advanced Usage
Chain multiple commands with -e
sed -e 's/foo/bar/g' -e 's/baz/qux/g' filename.txt
Use -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.
Insert a line after a match
sed '/pattern/a\New line to insert' filename.txt
The 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.

How to Use

  1. Open your terminal — sed is pre-installed on Linux and macOS.
  2. Run sed 's/pattern/replacement/g' file for a basic substitution preview.
  3. Verify the output is correct before adding the -i flag.
  4. Use -i.bak instead of plain -i when editing important files.
  5. Chain multiple -e expressions for complex transformations in one pass.

Frequently Asked Questions

What is the difference between sed and awk?

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.

Does sed modify the original file by default?

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.

Does sed work on macOS?

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.

Related Tools