The sed (Stream Editor) command in Linux is a powerful tool used for text processing and file modification. It allows users to search, replace, insert, and delete text in files efficiently, making it a vital command for automation and scripting. In this guide, we will explore how to use the sed command to update files in Linux with practical examples.


Why Use the Sed Command?

The sed command is widely used for:

Text Replacement – Easily replace words, phrases, or patterns in files.
Batch Processing – Modify multiple files at once using scripts.
Efficiency – Processes large files quickly without opening them.
Inline Editing – Modify files directly from the command line.
Automation – Used in scripts to handle repetitive text modifications.


Basic Syntax of the Sed Command

The general syntax of the sed command is:

sed [OPTIONS] 's/old-text/new-text/g' filename

Where:

  • s – Stands for substitution.
  • old-text – The text to be replaced.
  • new-text – The replacement text.
  • g – (Global) Ensures all occurrences in the line are replaced.
  • filename – The file to be modified.

How to Use Sed Command to Update Files

1. Replace a Word in a File

To replace a specific word in a file, use:

sed 's/Linux/Ubuntu/g' file.txt

This replaces the first occurrence of “Linux” with “Ubuntu” in each line of file.txt.

2. Replace All Occurrences in a File

To replace all occurrences of a word globally in a file:

sed 's/Linux/Ubuntu/g' file.txt

The g flag ensures that every occurrence in a line is replaced.

3. Edit a File in Place (Modify File Directly)

To make changes directly in the file without displaying the output:

sed -i 's/Linux/Ubuntu/g' file.txt

The -i option enables in-place editing, meaning the original file is modified without needing to save a separate version.

4. Replace Text Only in Specific Lines

To modify only specific lines in a file, specify the line number:

sed '3s/Linux/Ubuntu/' file.txt

This command replaces “Linux” with “Ubuntu” only on line 3.

5. Delete a Line Containing Specific Text

To delete all lines containing a specific word, use:

sed '/unwanted-text/d' file.txt

This removes all lines that contain “unwanted-text”.

6. Delete a Specific Line by Number

To remove a specific line, use:

sed '5d' file.txt

This deletes line 5 from file.txt.

7. Insert Text Before a Line

To add a line before a specific line number:

sed '3i\This is a new line' file.txt

This inserts “This is a new line” before line 3.

8. Insert Text After a Line

To insert a line after a specific line number:

sed '3a\This is an appended line' file.txt

This appends “This is an appended line” after line 3.

9. Replace a Word in Multiple Files

To replace text in multiple files at once:

sed -i 's/Linux/Ubuntu/g' *.txt

This command modifies all .txt files in the directory, replacing “Linux” with “Ubuntu”.


Advanced Sed Commands

1. Use Multiple Sed Commands Together

To perform multiple modifications at once:

sed -i -e 's/Linux/Ubuntu/g' -e 's/Server/Cloud/g' file.txt

This replaces “Linux” with “Ubuntu” and “Server” with “Cloud” in file.txt.

2. Use Sed with Regular Expressions

To replace numbers in a file:

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

This replaces all digits with #.

3. Extract Specific Lines from a File

To extract and display lines from 5 to 10:

sed -n '5,10p' file.txt

The -n option prevents printing the entire file, only showing lines 5-10.


Best Practices for Using Sed

🔹 Always backup files before using -i – Prevent accidental data loss.
🔹 Use regular expressions – Powerful pattern matching for complex modifications.
🔹 Test commands before applying changes – Run sed without -i to preview the output.
🔹 Use scripts for automation – Combine sed with shell scripting for batch processing.


Conclusion

The sed command is a versatile and efficient tool for updating files in Linux. Whether you need to replace text, delete lines, or insert content, sed can automate and simplify these tasks. By mastering sed, you can improve your workflow and enhance productivity in Linux administration and scripting.