Home Learn Linux sed Command in Linux: A Comprehensive Tutorial with Examples

sed Command in Linux: A Comprehensive Tutorial with Examples

The sed command, short for Stream Editor, is a powerful tool in Linux for text processing and manipulation directly within the command line. This guide offers a deep dive into sed usage with practical examples, from basic substitutions to advanced pattern matching, equipping you with the skills to efficiently handle text data.

by Arun Kumar
Published: Last Updated on
SED COMMAND USAGE

The sed command in Linux is a powerful tool for performing basic text transformations on an input stream (or “sed” stream). It’s a stream editor for filtering and transforming text, and it’s especially useful for tasks that involve searching, find and replace, insertion and deletion.

In this article, we will explore the basics of the sed command and provide some practical examples to help you understand how it works. We’ll cover some common use cases, such as searching for and replacing text, inserting and deleting lines, and using regular expressions to manipulate text.

What is sed?

At its heart, sed is a tool that reads input, performs some operation (or operations) on it, and then outputs the modified text. It’s primarily used for text substitution and more complex text transformations. The beauty of sed lies in its simplicity for straightforward tasks, yet it possesses the depth for complex pattern matching and editing scenarios.

sed command usage in Linux

Basic syntax

The basic syntax of sed is:

sed [options] 'command' file

Let’s start with something simple and build our way up, shall we?

Substituting text

One of the most common uses of sed is to substitute text. Here’s a basic example:

echo "Hello, world!" | sed 's/world/Linux/g'

Output:

Hello, Linux!

In this example, we’re piping the output of echo into sed, which substitutes “world” with “Linux”. The s in the command stands for substitute, world is our search pattern, Linux is the replacement text, and g is a flag that means “global”, telling sed to replace all instances in the line.

Deleting lines

Another handy feature is deleting lines. Let’s say we have a file named greetings.txt with the following content:

Hello, world!
Hello, Linux!
Hello, Unix!

To delete the second line, we use:

sed '2d' greetings.txt

Output:

Hello, world!
Hello, Unix!

The 2d command tells sed to delete the second line. Simple, right?

Adding lines

What about adding lines? Suppose we want to add a line after the first line in our greetings.txt:

sed '1a\
Hello, Sed!' greetings.txt

Output:

Hello, world!
Hello, Sed!
Hello, Linux!
Hello, Unix!

The 1a command tells sed to add (append) a new line after the first line.

In-place editing

One of the features I have a love-hate relationship with is in-place editing using the -i option. It’s incredibly useful for making changes directly in files, but remember to use it carefully (or with a backup option -i.bak) to avoid accidentally overwriting important data.

sed -i 's/Linux/GNU Linux/g' greetings.txt

This command replaces “Linux” with “GNU Linux” directly in the file. Remember, with great power comes great responsibility.

Advanced pattern matching

Sed also supports advanced pattern matching. For instance, you can use regular expressions to match patterns:

echo "The Linux kernel is awesome" | sed 's/Linux [a-zA-Z]*/& and GNU\/Linux/g'

Output:

The Linux kernel and GNU/Linux is awesome

Here, [a-zA-Z]* matches any word following “Linux”, and & in the replacement pattern refers to the matched pattern, allowing us to append text to it.

Working with multiple commands

One of the strengths of sed is the ability to execute multiple editing commands in a single pass through the data. This is particularly useful when you need to perform several transformations on a file. You can separate commands with a semicolon (;) or use multiple -e options.

Example:

echo -e "Welcome to Linux\nLinux is fun" | sed -e 's/Linux/GNU\/Linux/g' -e 's/fun/awesome/'

Output:

Welcome to GNU/Linux
GNU/Linux is awesome

In this example, we’ve replaced “Linux” with “GNU/Linux” and “fun” with “awesome” in a single command, showcasing how sed can efficiently handle multiple tasks.

The hold and pattern space

Sed operates using two main areas: the pattern space and the hold space. The pattern space is where sed reads the input line by line and performs operations. The hold space is a temporary storage where lines can be stored for later use. This concept is crucial for advanced sed operations, allowing for more complex data manipulation.

Swapping lines

Here’s an example that demonstrates using the hold space and pattern space to swap two lines:

echo -e "Line one\nLine two" | sed -n '1h;2x;1G;p'

Explanation:

  • 1h copies the first line to the hold space.
  • 2x exchanges the second line with the contents of the hold space.
  • 1G appends the hold space (the original first line) to the pattern space.
  • p prints the result.

Output:

Line two
Line one

This example shows the power of sed in manipulating line positions using the hold and pattern spaces.

Sed scripts

For complex text processing tasks, you can write sed commands in a script file. This is particularly useful for readability and maintenance when dealing with multiple sed operations.

Example sed_script.sed:

s/Linux/GNU\/Linux/g
s/fun/awesome/

Run the script on a file or input:

echo "Linux is fun" | sed -f sed_script.sed

Output:

GNU/Linux is awesome

Using scripts, you can organize and manage your sed commands more effectively, especially for complex transformations.

Address ranges

Sed can perform operations on specific lines or ranges of lines, giving you fine-grained control over which parts of the file are edited.

Example – Editing a range of lines:

sed '2,4s/Linux/GNU\/Linux/' greetings.txt

This command will only substitute “Linux” with “GNU/Linux” on lines 2 through 4 of the greetings.txt file. Address ranges can be line numbers, regular expressions, or a combination of both, providing a powerful way to target edits.

Sed command summary

This table provides a quick reference to some of the sed command patterns and operations we’ve explored.

Command Example Description
sed 's/world/Linux/g' Substitute “world” with “Linux” globally in a line.
sed '2d' Delete the second line of the file.
sed '1a\Hello, Sed!' Append “Hello, Sed!” after the first line.
sed -i 's/Linux/GNU Linux/g' Replace “Linux” with “GNU Linux” in the file directly (in-place editing).
sed 's/Linux [a-zA-Z]*/& and GNU\/Linux/g' Use advanced pattern matching to append text to a pattern.
sed -e 's/Linux/GNU\/Linux/g' -e 's/fun/awesome/' Execute multiple editing commands in a single pass.
echo -e "Line one\nLine two" | sed -n '1h;2x;1G;p' Swap two lines using the hold and pattern spaces.
sed -f sed_script.sed Run sed commands from a script file for complex operations.
sed '2,4s/Linux/GNU\/Linux/' Perform substitution on a specific range of lines (e.g., lines 2 through 4).

Conclusion

The sed command is a versatile and powerful tool for performing text transformations in Linux. With its ability to search, find, and replace text, insert and delete lines, and use regular expressions, sed can save you time and make your text processing tasks more efficient.

In this article, we’ve covered some of the basics of the sed command and provided some practical examples to help you understand how it works. From simple text substitution to more complex regular expression-based transformations, sed provides a wide range of capabilities for manipulating text.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.