Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Sed command in Unix?
3.
Syntax
4.
Applications
4.1.
Substituting String
4.2.
Substituting the nth Occurrence of the String
4.3.
Substituting all Occurrences of the String
4.4.
Substituting the nth to the Last Occurrence of a String 
4.5.
Parenthesise all Capital Letters
4.6.
Substituting on the nth Line
4.7.
Duplicating the Line with the Replaced String
4.8.
Printing only the Substituted Lines
4.9.
Substituting all the Strings in a given Line Range
4.10.
Substituting from the nth to the Last Line 
4.11.
Deleting nth Line
4.12.
Deleting the Last Line
4.13.
Deleting given Line Range
4.14.
Delete from the nth to the Last Line
4.15.
Deleting Lines with Matching Pattern
5.
Frequently Asked Questions
5.1.
What is the format of sed?
5.2.
What is the flag in the sed command?
5.3.
Does sed save the file?
5.4.
What is the difference between sed and grep?
5.5.
What is the difference between sed vs awk in Unix?
5.6.
Is sed only available in Unix-based operating systems?
5.7.
Can we overwrite the original file using sed?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Sed Command in Unix

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Hello Ninjas! Stream editing means modifying the text data stream in real time or when it is being processed. It is helpful when we need to perform a specific operation on a large amount of text data, such as replacing all occurrences of a particular word, deleting specific lines, or extracting certain information from the text. Instead of opening the text file in a text editor and manually editing it, stream editing allows you to perform these operations directly using commands in the command line without the need to save intermediate files.

Sed Command in Unix

One such command is the sed command in Unix. Let’s see its applications in detail.

What is Sed command in Unix?

The Sed command, short for 'stream editor,' is a potent tool for manipulating text data streams directly from the command line, avoiding the need to open text files. With Sed, users can execute a wide range of operations on text files, including tasks like searching and replacing words, inserting or deleting words and lines, and more. This utility supports regular expressions, making it particularly effective for pattern matching within text. Sed was introduced in Unix-based operating systems in 1973 when GUI-based editors were not yet prevalent. For additional insights into Unix and related topics, you can explore the content available on this blog.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax

The syntax for the sed command is as follows:

sed [options] 'command(s)' filename

 

Let’s break the syntax and understand the same-

  • The sed is the keyword of the command.
     
  • Options are keywords that modify the behaviour of the command. For example, ‘-n’ suppresses the output, and ‘-i’ is used to execute the command in place, etc. We will see the options in detail.
     
  • Command(s) are actual commands which tell what modifications are to be made in the text file.
     
  • The Filename is the name of the file that is to be modified.

Applications

Here we have shown various commands using suitable examples. Consider the following text, which is saved in the file “ninjatext.txt”:

Original text

Substituting String

We can replace or substitute another string at the first occurrence of the selected string in every line using the below command:

Command

sed 's/apple/orange/' ninjatest.txt

 

Output

Substituting String

Substituting the nth Occurrence of the String

We can replace another string at the nth occurrence of the selected string in every line using the below command:

Command

sed 's/apple/orange/n' ninjatest.txt

OR

sed 's/apple/orange/2' ninjatest.txt

 

Output

Substituting the nth occurrence of the string

Substituting all Occurrences of the String

We can replace another string at all occurrences of the selected string in every line using the below command:

Command

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

 

Output

Substituting all occurrences of the string

Substituting the nth to the Last Occurrence of a String 

We can replace another string from the nth to the last occurrence of the selected string in every line using the below command:

Command

sed 's/apple/orange/ng' ninjatest.txt

OR

sed 's/apple/orange/2g' ninjatest.txt

 

Output

Substituting the nth to the last occurrence of a string

Parenthesise all Capital Letters

We can parenthesise all the capital letters of the text using the below command:

Command

sed 's/\(\b[A-Z]\)/\(\1\)/g' ninjatest.txt

 

Output

Parenthesise all Capital letters

Substituting on the nth Line

We can replace all the occurrences of a string with the given string at the nth line using the below command:

Command

sed 'n s/apple/orange/' ninjatest.txt

OR

sed '2 s/apple/orange/' ninjatest.txt

 

Output

Substituting on the nth line

Duplicating the Line with the Replaced String

We can print duplicate lines which have replaced strings using the below command. The lines in which no substitution occurred will be printed only once. It uses the ‘/p’ command:

Command

sed 's/apple/orange/p' ninjatest.txt

 

Output

Duplicating the line with the replaced string

Printing only the Substituted Lines

We can print lines that have replaced strings. The lines without substitutions will not be printed using the below command. It uses the ‘/p’ command and the ‘/n’ option. The ‘/n’ option prevents the extra duplicated lines from being printed:

Command

sed -n 's/apple/orange/p' ninjatest.txt

 

Output 

Printing only the substituted lines

Substituting all the Strings in a given Line Range

We can replace the given string with another string in a given line range using the below command:

Command

sed '1,3 s/apple/orange/' ninjatest.txt

 

Output

Substituting all the strings in a given line range

Substituting from the nth to the Last Line 

We can replace the given string with another string from the nth to the last line using the below command:

Command

sed 'n,$ s/apple/orange/' ninjatest.txt

OR

sed '2,$ s/apple/orange/' ninjatest.txt

 

Output

Substituting from nth to last line

Deleting nth Line

We can delete the nth line using the below command:

Command

sed 'nd' ninjatest.txt

OR

sed '3d' ninjatest.txt

 

Output

Deleting nth line

Deleting the Last Line

We can delete the last line using the below command:

Command

sed '$d' ninjatest.txt

 

Output

Deleting last line

Deleting given Line Range

We can delete the given line range using the below command:

Command

sed '2,3d' ninjatest.txt

 

Output

 

Deleting given line range

Delete from the nth to the Last Line

We can delete nth to the last line using the below command:

Command

sed 'n,$d' ninjatest.txt

OR

sed '3,$d' ninjatest.txt

 

Output

Delete from nth to last line

Deleting Lines with Matching Pattern

We can delete the lines in which the given pattern occurs using the below command:

Command

sed '/apple/d' ninjatest.txt

 

Output

Deleting lines with matching pattern

Also see,  Open Source Operating System

Frequently Asked Questions

What is the format of sed?

The format of the `sed` command in Unix-like systems is: `sed [options] 'script' input-file`. It performs text manipulation based on the provided script.

What is the flag in the sed command?

In the `sed` command, flags are options that modify the behavior of the command. For example, `-i` is used for in-place editing.

Does sed save the file?

No, by default, the `sed` command does not save the file. It performs text manipulation on the input and prints the result to the standard output.

What is the difference between sed and grep?

Grep is the simplest of the three and is used for finding text patterns in a file. While Sed can find and modify data, it does have a slightly more complex syntax compared to grep.

What is the difference between sed vs awk in Unix?

Sed is primarily used for text editing and manipulation operations on files, such as search and replace, delete, insert, and more. In contrast, awk is a programming language with more advanced capabilities than sed. It has built-in functions for string and numeric manipulation, data filtering, conditional statements, and more.

Is sed only available in Unix-based operating systems?

No, sed is not only available in Unix-based operating systems. Sed is a cross-platform tool available in several operating systems, including Unix, Linux, macOS, Windows, and others. Regardless of the operating system, sed's basic syntax and functionality remain the same.

Can we overwrite the original file using sed?

Yes, the sed command can change the text in the actual file. By default, the sed command only outputs the modified text to the standard output, meaning the original file remains unchanged. However, we can use the ‘ -i’ option (or --in-place) with sed to modify the file directly.

Conclusion

In this article, we discussed the sed command in Unix. We also saw its application and how we can use it. By default, the sed command does not change the original file but can be made using the ‘-i’ option with sed. However, as good practice, keep a backup of the original file as the changes done by sed cannot be undone easily.

Read our other related articles:

 

You may refer to our Guided Path on Code Studios to enhance your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninjas!

Previous article
Features of Unix
Next article
Difference between Multiprogramming and Multitasking
Live masterclass