sed Command in Linux with Examples

The sed command in Linux is a powerful stream editor that allows you to perform basic text transformations on an input stream (a file or input from a pipeline). It is widely used for searching, finding and replacing, inserting, and deleting text within files. Unlike simple text editors, sed processes data in a pipeline and is especially useful for automating text manipulations in shell scripts.

Key features of sed include the ability to:

  • Substitute text within a file using regular expressions.
  • Delete lines based on specific patterns.
  • Insert or append text to specific lines.
  • Transform or convert text based on complex patterns.

This guide will provide examples to demonstrate the practical use of the sed command in Linux, showcasing how it can be utilized for various text-processing tasks.

Key Syntax and Options of the Sed Command

Before delving into examples, it’s vital to grasp the primary syntax of the sed command:

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

Here are a few commonly used options:

  • -e: Allows the use of multiple commands.
  • -n: Suppresses the default behavior when printing every line.
  • -i: Edits files in place (use caution as it modifies the original file).

Basic Examples of Sed Command in Linux

Basic Text Substitution

One of the most common uses of sed is to replace text.

echo "Welcome to Linux" | sed 's/Linux/Unix/'

Output:

Welcome to Unix

Global Substitution

By default, sed replaces the first occurrence in a line. To replace all occurrences, use the ‘g’ flag.

echo "Apples are better than apples" | sed 's/apples/oranges/g'

Output:

Apples are better than oranges

Line Deletion

sed can also remove lines based on a specific pattern.

echo -e "apple\norange\nbanana" | sed '/orange/d'

Output:

apple
banana

Inserting and Appending Text

You can introduce new text lines either before or after a pattern match.

echo "Hello" | sed 'i\Good Morning'

Output:

Good Morning
Hello

Changing Text Case

With sed, you can manipulate the text case using the y function. Let’s see an example of converting lowercase to uppercase:

echo "hello world" | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'

Output:

HELLO WORLD

Advanced Usage of the Sed Command in Linux

Multi-pattern Text Substitution

For multiple patterns, you can employ -e to chain various substitution commands.

echo "Welcome to Linux. I love Linux." | sed -e 's/Linux/Unix/' -e 's/love/adore/'

Output:

Welcome to Unix. I adore Linux.

Conditional Text Replacement

With sed, replacing text based on certain conditions using addresses is possible.

echo -e "apple\nbanana\ncherry" | sed '/banana/s/$/ - favorite fruit/'

Output:

apple
banana - favorite fruit
cherry

Using Sed with Regular Expressions

Harness the power of regular expressions to match complex patterns.

echo "Price is $100" | sed 's/$[0-9]*/& (USD)/'

Output:

Price is $100 (USD)

Back-references for Complex Replacements

Back-references can be utilized to refer back to parts of the matched pattern.

echo "The white cat and black dog" | sed 's/\(white\|black\) \(cat\|dog\)/\2 \1/'

Output:

The cat white and dog black

Editing In-Place with the -i Option

One of the powerful features of sed is the ability to edit files directly. This can be achieved using the -i option, but always back up the original file before making any direct modifications.

echo "Original text" > temp.txt
sed -i 's/Original/Modified/' temp.txt
cat temp.txt

Output:

Modified text

Conclusion

Understanding and mastering the sed command can significantly enhance your ability to manipulate and process text files in Linux. By practicing the examples provided, you can learn how to efficiently handle common text processing tasks, automate repetitive editing jobs, and incorporate sed into your scripting workflow. Regularly experimenting with different sed options and commands will deepen your knowledge and enable you to leverage its full potential. Enjoy the versatility and power that sed brings to your Linux text-processing tasks.

Leave a Comment