The sed command is a stream editor that helps you search, find, replace, insert, and delete strings from the referenced text file without opening it.
It is similar to the AWK and grep commands that follow the regular expression approach to modify the string from the text file.
Tutorial Details
Description | Sed (Stream Editor) |
Difficulty Level | Moderate |
Root or Sudo Privileges | Maybe |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | sed |
Internet Required | No |
Sample Text File
In this article, you will learn the sed command with the help of the following text file.
$ cat file.txt
Output:
All the changes you made in this file will only be visible in the output; the file itself will not be modified; however, you can use the redirection symbol β>>
β to save the changes in a new file.
Find and Replace String from File
You can easily find a string from the file and replace it with something else, as shown.
$ sed 's/line/sentences/' file.txt
The above command will replace all the βlineβ strings with βsentencesβ strings.
The above changes will not show up in the referenced text file, but you can use the redirection symbol to make a new text file with the changes.
$ sed 's/line/sentences/' file.txt >> newfile.txt
The above command will create and save the changes in a new text file with the name βnewfile.txt
β.
Replacing the nth Occurrence of a Pattern in a Line
In the previous example, only the first occurrence of βlineβ was replaced with the βsentencesβ string. However, you can replace the string if it appears a second or third time.
$ sed 's/line/sentences/2' file.txt
Output:
The above command will replace the second instance of βlineβ with the string βsentencesβ.
$ sed 's/line/sentences/3' file.txt
Output:
The above command will replace the third instance of βlineβ with the string βsentencesβ.
Replacing from nth Occurrence to All Occurrences in a Line
The following command will replace all the strings after the second occurrence of βlineβ with the βsentencesβ string.
$ sed 's/line/sentences/2g' file.txt
Output:
Replacing All the Occurrences of a Pattern in a Line
If you want to replace all the βlineβ with the βsentencesβ string from your file, then use the β/g
β flag as shown.
$ sed 's/line/sentences/g' file.txt
Output:
Replacing a String on a Specific Line Number
Use the following command to change the string βlineβ on the second line to the string βsentencesβ.
$ sed '2 s/line/sentences/' file.txt
Output:
Replacing All the Strings on a Specific Line Number
The following command will replace all the βlineβ strings with βsentencesβ strings in the second line.
$ sed '2 s/line/sentences/g' file.txt
Output:
Replacing the String on a Range of Lines
You can specify the range of lines (like the second and third line) where the string should be replaced with the specified string.
$ sed '2,3 s/line/sentences/' file.txt
Output:
Replacing the Last Match Line with New Text
The following command will only replace the last line if it matches the pattern.
$ sed -e '$s/file/line/' file.txt
Output:
Duplicate the Replaced Line
The following command will duplicate the replaced line with a new line.
$ sed 's/line/sentences/p' file.txt
Output:
Only Print the Replaced Line
Use the following command to output only the modified lines using the β-n
β flag.
$ sed -n 's/line/sentences/p' file.txt
Output:
Deleting Lines from a File
Specify the integer (referring to a line number) with the βd
β flag to remove the specific line from the file, as shown.
$ sed 2d file.txt
The above command will remove the second line from the file.
Replace the β2d
β with β$d
β to delete the last line.
$ sed '$d' file.txt
Output:
Use the following command to specify the range of lines you need to delete.
$ sed '2,3d' file.txt
The above command will remove all the lines between the second and third lines.
To delete all the lines after a second line, use the following command.
$ sed '2,$d' file.txt
Output:
The following command will remove the lines matching the βlineβ string.
$ sed '/line/d' file.txt
Output:
Thatβs the last example of the βsed
β command.
If you have anything more to add to this article, then let us know in the comment section.
Iβve been using the sed command for long but I should say awk is more effective if you know it well