The full form of the “sed” command is a stream editor. This command is a very powerful command of the Linux operating system and it is used for various purposes. One of the common uses of this command is to find and replace one or more string values of a file. The particular content of a file can be replaced by this command without opening the file. The “sed” command supports regular expression. The specific string value can be searched inside a file based on the regular expression which is defined with this command. The different ways of searching and replacing the particular string in a file using the “sed” command are shown in this tutorial.
Syntax:
The syntax of the “sed” command to search and replace the string from a file is given as follows:
Here, the “i” option is used to replace the content of the original file if the searching word is found. The “s” command indicates the substitute that searches the particular string and replaces the string with another string if the searching word exists in the file. The filename contains the name of any existing filename where the search and replacement are done.
Different Examples of “Sed” Commands
The different ways of searching and replacing the strings in a file are shown in this part of the tutorial. Create a text file named students.txt by the following content where the search and replace task are done using the “sed” command:
Md. Hossain:CSE:Batch-50:Semester-10
Nibir Rahman:CSE:Batch-51:Semester-9
Mehnaz Kazi:CSE:Batch-52:Semester-8
Example 1: Replace All Occurrences of the String without Changing the File
The method of replacing a particular string in a file based on the exact match of the search string is shown in this example.
Run the following command to check the content of the students.txt file:
Run the following command to search the word “CSE” in the students.txt file and replace all occurrences of this string using “BBA”:
Output:
The following output appears after executing the previous commands:
Example 2: Permanently Replace All Occurrences of the String in the File
The method of permanently replacing a particular string in a file based on the search string is shown in this example.
Run the following command to check the content of the students.txt file:
Run the following command to search the word “batch” in the students.txt file and replace all occurrences of this string in the file permanently using “BATCH”:
Run the following command to check the updated content of the students.txt file:
Output:
The following output appears after executing the previous commands:
Example 3: Replace the Particular Occurrence of the String in the File
The method of replacing a particular string of a particular occurrence in a file based on the exact match of the search string is shown in this example:
Run the following command to check the content of the students.txt file:
Run the following command to search the word “0” in the students.txt file and replace all occurrences of this string in the file using “1”:
Run the following command to search the word “0” in the students.txt file and replace the second occurrence of this string in the file using “1”:
Output:
The following output appears after executing the previous commands:
Example 4: Print the Replaced Lines of the File Two Times
The method of replacing a particular string in a file based on the exact match of the search string and printing each replaced line two times is shown in this example.
Run the following command to check the content of the students.txt file:
Run the following command to search the word “BATCH” in the students.txt file and replace all occurrences of this string in the file using “Batch” and print the replaced lines two times:
Output:
The following output appears after executing the previous commands:
Example 5: Replace the String of the Particular Line of the File
The method of replacing a particular string of a particular line in a file based on the search string is shown in this example.
Run the following command to check the content of the students.txt file:
Run the following command to search the word “BATCH” in the students.txt file and replace the occurrence of this string in the second line of the file using “Batch”:
Output:
The following output appears after executing the previous commands:
Example 6: Replace the String of the Particular Range of Lines of the File
The method of replacing a particular string in the particular range of lines of a file based on the search string is shown in this example.
Run the following command to check the content of the students.txt file:
Run the following command to search the word “BATCH” in the students.txt file and replace the occurrence of this string from the first to the second line of the file using “Batch”:
Output:
The following output appears after executing the previous commands:
Conclusion
The “sed” command is a very useful command of Linux to do the search and replace task. It makes the task of replacing the content of a file easier using different search patterns. The uses of the “sed” command to replace the content of a file based on the exact match, the number of search occurrences, or the number of the line are shown here by multiple examples. We hope that Linux users are able to use the “sed” command to replace the file content after reading this tutorial properly.