sed Command in Linux with Examples

Linux, a versatile open-source operating system, offers various command-line utilities for various tasks. The sed command, standing for Stream Editor, holds a significant place among these utilities. It’s specifically tailored for parsing and transforming text in a data stream or file. This article unveils the depth and breadth of the sed command in Linux, elucidating its functionalities through concrete examples.

Understanding the Sed Command

What is the Sed Command?

Short for Stream Editor, the sed command is a potent and flexible text manipulation tool. It primarily works by processing text line by line, making it especially suitable for tasks like text substitutions, deletions, and insertions.

Why Use the Sed Command in Linux?

  1. In-place Editing: Unlike some text manipulation commands, sed can modify files directly.
  2. Scriptable: For routine tasks, you can script sed commands, enabling automation.
  3. Versatile Text Manipulation: Beyond basic replacements, sed can handle more complex transformations using regular expressions.

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 of printing every line.
  • -i: Edits files in place (use with 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

In sed, you can manipulate the text case using the y function. Let’s see an example to convert 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, it’s possible to replace text based on certain conditions using addresses.

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 remember to 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

The sed command in Linux is a robust stream editor that can transform text in intricate ways with minimal input. Its adaptability, combined with the power of regular expressions, makes it an essential tool in the arsenal of every Linux user. Whether you’re performing simple text replacements or complex pattern-based edits, sed provides the capability to get the job done swiftly. As you become more accustomed to it, you’ll discover even more advanced features and tricks to streamline your text processing tasks. Remember, as with all powerful tools, it’s crucial to use sed wisely, especially when editing files in-place.