Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

sed Command in Linux Unix With Examples

Last Updated on December 11, 2023 by Abhishek Sharma


The sed command, short for stream editor, is a powerful utility in Linux and Unix-like operating systems designed for text processing and manipulation. Whether you’re searching and replacing text, deleting specific lines, or performing more complex text transformations, sed is an invaluable tool for streamlining your text-editing tasks. In this article, we’ll explore the fundamentals of the sed command, its syntax, and provide practical examples to showcase its versatility.

What is Sed Command in Linux?

sed operates by reading input from a file or a pipeline, processing the data line by line, and then printing the modified text to the standard output. While sed supports a variety of commands and options, its most common use cases involve search and replace operations.

Basic Syntax of Sed Command in Linux

The basic syntax for a sed command is as follows:

  • sed [options] ‘command’ filename
  • Options: Modify the behavior of sed. For example, the -i option enables in-place editing.
  • Command: Specifies the operation to be performed on the text.
  • Filename: The name of the file to be processed.

Practical Examples of Sed Command in Linux

Here are some examples of Sed Command in Linux:

1. Search and Replace
The most common use of sed is to search for a specific pattern and replace it with another. In this example, we replace all occurrences of "apple" with "orange" in a file named fruits.txt:

sed 's/apple/orange/g' fruits.txt

2. In-Place Editing
To perform an in-place edit (modify the file directly), use the -i option. The following command replaces "old" with "new" in a file named example.txt:

sed -i 's/old/new/g' example.txt

3. Deleting Lines
To delete lines that match a specific pattern, use the d command. The following example deletes all lines containing the word "remove" from data.txt:

sed '/remove/d' data.txt

4. Adding and Appending Text
You can add or append text to lines that match a pattern. Here, we add "Start:" at the beginning of lines containing "Task":

sed '/Task/ s/^/Start: /' tasks.txt

5. Printing Specific Lines
To print specific lines, use the p command. The following command prints lines containing the word "error" twice:

sed -n '/error/ p' log.txt

6. Transforming Text with Regular Expressions
sed supports regular expressions for more complex pattern matching. In this example, we replace all occurrences of numbers with "NUM" in a file:

sed 's/[0-9]+/NUM/g' data.txt

7. Using Multiple Commands
Combine multiple sed commands in a single operation. This example replaces "apple" with "orange" and then deletes lines containing "banana":

sed -e 's/apple/orange/g' -e '/banana/d' fruits.txt

Conclusion
The sed command is a versatile and efficient tool for text processing in Linux and Unix environments. By mastering its commands and options, you can easily perform complex text manipulations, automate editing tasks, and streamline your workflow. Whether you’re a system administrator, developer, or power user, sed is an essential addition to your toolkit for efficient text editing on the command line. Experiment with different commands and options to unlock the full potential of sed in your day-to-day tasks.

FAQs related to set Command in Linux

Below are FAQs related to set Command in Linux:

1. How does sed differ from other text processing tools?
sed is specifically designed for streamlining and editing text in a pipeline. It excels at search and replace operations, deletion or addition of lines, and applying transformations based on specified patterns. Other tools, such as awk or grep, have different focuses and capabilities.

2. What is the basic syntax of the sed command?
The basic syntax of sed is: sed [options] ‘command’ filename. Options modify the behavior, the command specifies the text operation, and filename is the name of the file to be processed.

3. How can I perform in-place editing with sed?
To perform in-place editing and modify the file directly, use the -i option. For example: sed -i ‘s/old/new/g’ example.txt.

4. How can I delete specific lines with sed?
To delete lines that match a specific pattern, use the d command. For example: sed ‘/pattern/d’ filename.

5. Can sed handle regular expressions?
Yes, sed supports regular expressions for pattern matching and manipulation. Regular expressions provide powerful and flexible ways to define search patterns.

6. How can I print specific lines with sed?
To print specific lines, use the p command. For example: sed -n ‘/pattern/ p’ filename.

Leave a Reply

Your email address will not be published. Required fields are marked *