Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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”:
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 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 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 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
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
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
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
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
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 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
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 the Last Line
We can delete the last line using the below command:
Command
sed '$d' ninjatest.txt
Output
Deleting given Line Range
We can delete the given line range using the below command:
Command
sed '2,3d' ninjatest.txt
Output
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
Deleting Lines with Matching Pattern
We can delete the lines in which the given pattern occurs using the below command:
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.