Sed Command in Unix/Linux 

Mon Sep 25, 2023

Introduction         

In the Linux and Unix world, we have a handy tool called sed. Whether you're a sysadmin, a developer, or just someone who deals with text files, sed is a powerful tool for text editing and manipulation. It makes text tasks in Linux easier and smarter. Let's explore how sed can simplify your text editing.

What is sed?       

Sed stands for "Stream Editor." It's a command-line utility used for text processing and editing in Unix-like operating systems, including Linux.

Basic Sed Syntax       

sed [options] "command" <input-file>

we will explore more about options and commands of sed in the Hands-on Section. 

What you can do with sed in Unix/Linux?       

Here are some common tasks you can accomplish with the "sed" command:

  1. Search and Replace: You can find specific text patterns within a file and replace them with something else.
  2. Delete Lines: You can delete specific lines from a file. 
  3. Add or Insert Text: You can insert new text or lines at specific positions in a file.
  4. Text Transformation: Sed can perform various text transformations, such as converting text to uppercase or lowercase, numbering lines, or reversing the order of characters.
  5. Text Filtering: You can use "sed" to filter lines that match specific patterns and output only those lines. This is useful for extracting specific information from a file.
  6. Conditional Editing: Sed can perform conditional editing based on specific criteria. For example, you can replace text only on lines that meet certain conditions.
  7. Multiple Edits: You can chain multiple "sed" commands together to perform a series of text manipulations in a single pass through a file.
  8. Regular Expressions: Sed supports powerful regular expressions, allowing for complex pattern matching and manipulation.
  9. In-Place Editing: You can edit a file in-place, meaning the changes are saved directly to the original file without creating a new one.
  10. Batch Processing: "sed" is excellent for automating text processing tasks across multiple files. You can use it in scripts to process large sets of files simultaneously.

Hands-On: Get Our Hands Dirty with Sed

1. Search and Replace:

Let's walk through a hands-on example of how to use the "sed" command in Linux to search for a specific text pattern and replace it with something else in a text file.

Suppose you have a text file named "text.txt" with the following content

Now let us replace Sampath with SampathSivaKumar using the sed command.

Note: When you run the above command, it doesn't modify the original file directly. Instead, it prints the modified content to the standard output. If you want to save the changes back to the original file, you can use the -i flag like this:   

You can now see the changes were applied to the original file itself. The command 

sed -i "s/Sampath/SampathSivaKumar/" text.txt does the following:

  • sed: Invokes the sed command for text manipulation.
  • -i: This flag tells sed to edit the file in place (i.e., make changes directly to "text.txt").
  • "s/Sampath/SampathSivaKumar/": Specifies the substitution operation in sed. It finds the text "Sampath" and replaces it with "SampathSivaKumar."
  • text.txt: The name of the text file you want to edit.
2. Delete Lines: 

Suppose you have a text file named "text.txt" with the following content                                                         2.1 Using the sed command let's try to delete the empty lines present in the file.                        

let's break down the sed command sed '/^$/d' text2.txt into its components:

  • sed: This is the command for the stream editor.
  • /^$/: This part enclosed in slashes is a regular expression pattern. It specifically matches empty lines. Here's what it means:
    • ^: Matches the start of a line.
    • $: Matches the end of a line.
    • Combined, ^$ matches lines that have nothing between the start and end, in other words, empty lines.
  • d: This is the sed command to delete lines.
  • text2.txt: This is the name of the file you want to process. In this case, it's "text2.txt."                                                                                           

2.2  Deleting Lines by Line Number

Suppose you have a text file named "text3.txt" with the following content

Now let us delete the 5th line using the sed command.

Certainly, let's break down the sed command sed "5d" text3.txt into its components:

  • sed: This is the command for the stream editor.
  • 5d: This part is a sed command that deletes the 5th line.
  • text3.txt: This is the name of the file you want to process. In this case, it's "text3.txt."
So, when you put it all together, the sed "5d" text3.txt command tells sed to delete the 5th line in the "text3.txt" file.

2.3 Deleting Lines in a Range


Let us try to delete lines 3 to 7 from the text3.txt using the sed command.

let's break down the sed command sed "3,7d" text3.txt into its components:

  • sed: This is the command for the stream editor.
  • 3,7d: This part is a sed command that deletes lines from the 3rd to the 7th line (inclusive).
  • text3.txt: This is the name of the file you want to process. In this case, it's "text3.txt."
So, when you put it all together, the sed "3,7d" text3.txt command tells sed to delete lines 3 through 7 in the "text3.txt" file.

3. Add or Insert Text:

3.1 Insert Text at the Beginning of Each Line

Suppose you have a text file named "text3.txt" with the following content

Let us try to insert "Hello all " at the beginning of every line present in text3.txt using the sed command.

 

3.2 Insert Text After a Specific Pattern

Suppose you have a text file named "text3.txt" with the following content

Now let us try to insert line no after line in the entire file text3.txt using the sed command.

3.3 Insert Text After a Specific Line

Suppose you have a text file named "text3.txt" with the following content 

Let us try to insert this is line-10 after the 9th line in text3.txt file.

let's break down the sed "9a\this is line-10" text3.txt command:

  • sed: This is the command for the stream editor.
  • "9a\this is line-10": This is the sed command.
    • 9: Specifies the line number (in this case, the 9th line).
    • a: Stands for "append." It tells sed to append text after the specified line.
    • \this is line-10: The text you want to insert, which is "this is line-10" in this case.
  • text3.txt: This is the name of the file you want to process. In this command, it's "text3.txt."
So, when you run the sed "9a\this is line-10" text3.txt command, it inserts the line "this is line-10" after the 9th line in the "text3.txt" file, leaving the original contents unchanged. if you want to change the original contents of the file use the -i option.

4. Text Transformation:

Let us Convert Uppercase to Lowercase using the sed command. Suppose you have a file named "data.txt" with text in uppercase, and you want to convert it to lowercase. You can use sed to achieve this transformation:


Now let us try to Convert Uppercase to Lowercase using the sed command 

5. Filtering Lines Containing a Specific Word:

Suppose you have a file named "sample.txt," and you want to filter and display only the lines that contain the word "apple." You can use sed to achieve this:


let us try to print only the lines containing the word "apple"

As you can see, only the lines that match the pattern "apple" are displayed, thanks to the filtering capability of sed.

6. Conditional Editing:

Suppose you have a file named "data.txt" with lines of text, and you want to add the prefix "Processed: " to lines that contain the word "important." You can use sed with a conditional statement to achieve this:


When you run the sed '/important/s/^/Processed: /' data.txt command, it will modify the lines that contain "important" and add the "Processed: " prefix:

As you can see, the prefix "Processed: " was added only to the lines that contained the word "important," demonstrating how conditional editing in sed allows you to selectively modify text based on specific conditions or patterns.

Conclusion:      

In this blog, we've explored the sed command and its applications in Unix/Linux. 

You don't need to memorize all the options and commands of sed; instead, aim to grasp the concepts at a high level. 

If you ever need to reference sed's options and commands, you can easily access them using the "man" command in your terminal.

I hope this blog has provided valuable insights and assistance for your text-processing tasks in Unix/Linux.

SampathSivaKumar Boddeti

AWS & Terraform Certified

LinkedIn