Linux Terminal Command: sed

The sed command is an essential tool in Text Processing & Piping. In this tutorial, we will explore what sed does, look at everyday examples, and cover advanced options to supercharge your command-line workflow.


Concept & Explanation

The sed command performs basic text transformations on an input stream (a file or input from a pipeline) using regular expressions.


Common Options & Syntax

sed [options] [arguments]

Here are the most common flags used with sed:


1. Interactive Example (Simple)

Here is how most people run the command:

# Example
sed 's/old/new/g' file.txt

What it does: Replaces all occurrences of ‘old’ with ’new’ in ‘file.txt’ and outputs the result to screen.


2. Power-User Example (Advanced)

For scripting and advanced diagnostics, use this configuration:

# Advanced
sed -i.bak '/^#/d; s/localhost/127.0.0.1/g' config.txt

What it does: Modifies ‘config.txt’ in-place (-i), creating a backup named ‘config.txt.bak’, deletes comment lines (/^#/d), and replaces ’localhost’ with the IP.


⚙️ Warning & Common Pitfalls

[!WARNING] sed regex engines vary by platform. On macOS (BSD), the -i flag requires an explicit backup suffix argument (e.g. sed -i '' 's/a/b/'), whereas GNU sed (Linux) does not.


Here are some related posts on cli_tty1 you might want to check out: