Top 10 SED Use Cases Every Linux User Should Know

Oliver Bennet
6 min readNov 5, 2024

The Stream Editor (sed) is one of the most powerful text-processing tools in Unix and Linux. With sed, you can automate text transformations and streamline tasks, from simple substitutions to complex multi-line edits. Here, we’ll explore the top 10 sed use cases, with practical examples to help you work efficiently from the command line.

1. Basic Text Substitution

A common use of sed is replacing text in files. You can change all occurrences of a word, phrase, or pattern with another—useful for tasks like renaming terms in documents or code.

Example: Replace all occurrences of “cat” with “dog” in pets.txt.

sed 's/cat/dog/g' pets.txt

In this command:

  • s denotes the substitution.
  • cat is the text to replace.
  • dog is the new text.
  • g ensures all occurrences on each line are replaced. Without it, only the first instance on each line changes.

Why it’s useful: Simple substitutions are essential for text file automation, especially when replacing repetitive terms across long documents or scripts.

2. Replace Text Only on Specific Lines

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Oliver Bennet
Oliver Bennet

Written by Oliver Bennet

20 Years of Open-Source Experience. Currently I Write about DevOps, Programming and Linux Technologies.

No responses yet

What are your thoughts?