Cloud Service >> Knowledgebase >> How To >> How to Use the Linux sed Command in 2025 With Useful Examples
submit query

Cut Hosting Costs! Submit Query Today!

How to Use the Linux sed Command in 2025 With Useful Examples

"This article explores the functionality and usage of the Linux sed command, a powerful stream editor for text manipulation in files or input streams. The content covers the basics of sed, its syntax, and various practical examples, including search and replace, text insertion, and deletion. It also delves into advanced usage, such as working with regular expressions and multiple files. Alongside, it provides clear explanations, useful tips, and suggested images for better understanding."

The Linux sed command is an essential utility for text manipulation, widely used by system administrators, developers, and data analysts. Whether you're editing configuration files or processing large datasets, sed can streamline repetitive tasks with ease and efficiency. This article covers the fundamental aspects of sed and demonstrates practical examples to help you use it effectively in 2025.

What Is the sed Command?

sed, short for Stream Editor, is a command-line utility for parsing and transforming text. It works as a filter, allowing users to process and modify input streams (like files or pipelines) based on defined patterns and rules.

Key Features of sed

Search and Replace: Modify text strings in files or data streams.

Text Insertion and Deletion: Add or remove lines efficiently.

Batch Processing: Edit multiple files simultaneously.

Regular Expressions: Handle complex patterns for advanced text editing.

Basic Syntax

The general syntax for the sed command is:

 

sed [OPTIONS] 'SCRIPT' [FILE...]

OPTIONS: Flags to modify behavior (e.g., -n, -i).

SCRIPT: The editing instructions (e.g., s/old/new/g).

FILE: The file(s) to process.

Practical Examples of Using sed

1. Search and Replace Text

To replace all occurrences of "2024" with "2025" in a file:

 

sed 's/2024/2025/g' file.txt

Explanation:

s: Substitution command.

/g: Global replacement (replace all matches).

2. Replace Text in Multiple Files

To update "error" to "issue" in all .log files:

 

sed -i 's/error/issue/g' *.log

Note: The -i option edits files in place.

3. Delete Lines

To remove line 3 from a file:

sed '3d' file.txt

4. Insert Text

To add a header line at the start of a file:

sed '1i Header: Log File' file.txt

5. Use Regular Expressions

To extract email addresses from a file:

 

sed -n '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/p' file.txt

Explanation:

-n: Suppresses default output.

/pattern/p: Prints lines matching the pattern.

6. Replace Text in a Pipeline

To replace "Linux" with "Unix" in streamed data:

 

echo "I love Linux" | sed 's/Linux/Unix/'


Advanced sed Usage

1. Process Multiple Patterns

To replace multiple strings in a single command:

 

sed -e 's/foo/bar/g' -e 's/baz/qux/g' file.txt

2. Append Text to Specific Lines

To append text after line 5:

 

sed '5a Appended Text' file.txt


3. Edit Files Based on Conditions

To delete lines containing "debug":

 

sed '/debug/d' file.txt


Benefits of Using sed

Efficiency: Handles large files and repetitive tasks quickly.

Flexibility: Supports complex text manipulations with regular expressions.

Integration: Works seamlessly with other Linux utilities like awk, grep, and cut.

Fun Fact

sed was introduced in 1973 and remains a cornerstone of UNIX-based systems, proving its enduring relevance in text processing.

 

Conclusion

The sed command is a versatile and powerful tool for text editing, suitable for both beginners and experienced users. By mastering its basic and advanced features, you can simplify a wide range of tasks, from text manipulation to data cleaning. Whether you're managing configuration files or automating workflows, sed will remain indispensable in 2025 and beyond.

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!