Methods of payment Abuse

The sed Command in Linux: A Complete Guide to Automated Text Processing

20.03.2025, 14:00

The sed command is a command-line utility used for:
 → Automatic file editing
 → Replacing text fragments based on specified rules
 → Deleting, inserting, and modifying lines
 → Executing complex scripts without manual intervention

This makes sed an ideal program for automating text modifications.

Whether you are a beginner Linux user or an experienced system administrator, knowing sed will help you:
→ Automate routine tasks, such as bulk replacement of values in configuration files or logs.
→ Speed up configuration file editing, allowing you to modify parameters without manually opening files.
→ Simplify working with text data, for example, converting files from one format to another.

In this guide, we will cover:
→ The syntax and commands of sed
→ Methods for replacing text and numbers
→ Using regular expressions

How Does sed Work?

sed is not just a command-line utility; it is a stream editor. Its key feature is processing line by line, performing specified operations, and outputting the result without modifying the original file unless explicitly instructed to do so.

Unlike text editors like vim or nano, sed operates non-interactively, meaning all changes happen automatically without manual editing.

Basic Workflow of sed:
1. Reading input data (file or stream).
2. Processing data according to specified rules.
3. Outputting data.

💡 Note: By default, sed does not modify the file. To save changes, use the -i (in-place) option.

Example of sed Editing a File In-Place

sed -i 's/old_text/new_text/g' file.txt

This command replaces all occurrences of "old_text" with "new_text" directly in file.txt.

Key Features of sed

→ Search and replace text or numbers
→ Delete, insert, and modify lines
→ Automatically process files and data streams
→ Dynamically modify file contents based on conditions
→ Group commands for more complex processing scenarios
→ Process multiple files simultaneously

General Syntax of the sed Command

sed [option] 'command' file

Syntax Breakdown
→ option – Parameters that modify sed behavior.
→ command – The action to perform (e.g., text replacement).
→ file – The file to process (if not specified, sed reads from standard input).
💡 Important! If sed receives data via echo or cat, it applies the command to each line separately.

Simple Text Replacement Example

echo "I like apple pie." | sed 's/apple/orange/'

Output:

I like orange pie.

Multiple Replacements in a File
If you need to replace multiple text fragments at once, use -e to specify multiple commands:

sed -e 's/apple/orange/g' -e 's/pie/cake/g' file.txt

This command replaces "apple" with "orange" and "pie" with "cake" throughout the file.

Dynamic Number Replacement Using sed

1. Simple Number Replacement
To replace a specific number with another, use the s (substitute) command:

sed 's/old_number/new_number/' file.txt

Example: Replace 100 with 200

echo "The price is 100 dollars." | sed 's/100/200/'

Output:

The price is 200 dollars.

2. Replace All Numbers in a Line
To replace any number in a line with a specific value, use regular expressions:

sed 's/[0-9]\+/0/g' file.txt

Example:

echo "The items cost 100, 200, and 300 dollars." | sed 's/[0-9]\+/0/g'

Output:

The items cost 0, 0, and 0 dollars.

3. Increase All Numbers in a Line
To increase all numbers in a line by a certain value, combine sed with bash:

echo "Item 1 costs 100, item 2 costs 200." | sed -E 's/[0-9]+/echo $((\0 + 10))/ge'

Output:

Item 1 costs 110, item 2 costs 210.

Conclusion

Using sed, you can perform complex text operations, significantly simplifying file editing and processing.

💡 Key Takeaways:
 → How to use sed for search and replace.
 → How to dynamically replace numbers.
 → How to work with regular expressions.
 → How to modify files in-place.

Now you are ready to automate text processing in Linux!