ArrowLeft Icon

Mastering Text Manipulation with the Linux sed Command

📆 · ⏳ 3 min read · · 👀

Introduction

The sed command is a stream editor that allows you to perform text manipulation operations on input streams. It is a command-line utility that is available on most Unix-like systems, including Linux.

sed is particularly useful for automating text editing tasks, such as replacing or deleting text, and for transforming text in complex ways.

Overview of the sed command

The sed command works by reading an input stream, applying a set of commands to each line of the input, and then outputting the modified text. The basic syntax of the sed command is as follows:

Terminal window
sed [options] 'command' input_file

The options are used to modify the behavior of the sed command, while the command specifies the text manipulation operations to be performed. The input_file parameter specifies the name of the file to be processed.

Text manipulation operations

The sed command supports a wide range of text manipulation operations. Here are some examples of sed commands:

Searching for patterns

The following command searches for lines that contain the word “Linux” and prints them to the console:

Terminal window
sed -n '/Linux/p' input_file

The -n option suppresses the default output, while the /Linux/p command searches for lines that contain the word “Linux” and prints them to the console.

Replacing text

The following command replaces all occurrences of the word “Linux” with the word “Unix” and writes the output to a new file:

Terminal window
sed 's/Linux/Unix/g' input_file > output_file

The s command stands for “substitute” and the g option stands for “global”, which means that all occurrences of the pattern should be replaced.

Deleting text

The following command deletes all lines that contain the word “Linux”:

Terminal window
sed '/Linux/d' input_file > output_file

The d command stands for “delete” and the /Linux/ pattern searches for lines that contain the word “Linux”.

Adding text

The following command adds a line of text after every line that contains the word “Linux”:

Terminal window
sed '/Linux/a\This is a new line' input_file > output_file

The a command stands for “append” and the backslash is used to escape the newline character.

Inserting text

The following command inserts a line of text before every line that contains the word “Linux”:

Terminal window
sed '/Linux/i\This is a new line' input_file > output_file

The i command stands for “insert” and the backslash is used to escape the newline character.

Modifying specific lines

The following command replaces the first occurrence of the word “Linux” with the word “Unix” on the third line of the input file:

Terminal window
sed '3s/Linux/Unix/' input_file > output_file

The 3s command specifies that the operation should be performed on the third line of the input file.

Using regular expressions

The following command replaces all occurrences of the word “Linux” or “Unix” with the word “Operating System”:

Terminal window
sed 's/Linux\|Unix/Operating System/g' input_file > output_file

The | character is used to specify multiple patterns, and the g option stands for “global”, which means that all occurrences of the pattern should be replaced.

Conclusion

The sed command is a powerful tool for text manipulation on Linux systems. With sed, you can automate repetitive text editing tasks and transform text in complex ways.

By mastering the sed command, you can save time and effort when working with large amounts of text.

EnvelopeOpen IconStay up to date

Get notified when I publish something new, and unsubscribe at any time.

Need help with your software project? Let’s talk

You may also like

  • # linux

    Easily Backup your Local Data to the Cloud in Linux

    In this digital age, securing your precious data is non-negotiable. Let's delve into the world of continuous backups using a nifty tool called rclone, perfect for tech-savvy folks. We'll even set up a backup with Google Drive as an example. Your data's future is in good hands!

  • # linux

    Linux System Logs: An Overview of System Logs and How to Read Them

    Have you ever wondered where all the information about your system's activities and events is stored? Linux system logs contain a wealth of information that can help diagnose and troubleshoot system issues. In this article, we will give an overview of Linux system logs and explain how to read and interpret them.

  • # linux

    Linux RAID Configurations for Data Redundancy and Performance

    RAID is a popular method of combining multiple physical storage devices into a single logical unit, for the purposes of improving data redundancy and/or performance. Linux has a number of built-in tools and configurations for managing RAID setups. In this article, we'll provide an overview of RAID and the different RAID levels, as well as examples of how to configure and manage RAID arrays in Linux.