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:
![Sample file content](https://linuxtldr.com/wp-content/uploads/2022/11/Sample-file-content-1024x224.webp)
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.
![Find and replace the string in the text file](https://linuxtldr.com/wp-content/uploads/2022/11/Find-and-replace-the-string-from-the-text-file-1024x218.webp)
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:
![Replace the string on a specific occurrence](https://linuxtldr.com/wp-content/uploads/2022/11/Replace-the-string-on-specific-occurrence-1024x215.webp)
The above command will replace the second instance of “line” with the string “sentences”.
$ sed 's/line/sentences/3' file.txt
Output:
![Replace the string on the third occurrence](https://linuxtldr.com/wp-content/uploads/2022/11/Replace-the-string-on-third-occurrence-1024x218.webp)
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:
![Replace all occurrences starting with the nth.](https://linuxtldr.com/wp-content/uploads/2022/11/Replace-from-nth-to-all-occurrences-1024x231.webp)
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:
![Replace all the occurrences of a pattern in a line](https://linuxtldr.com/wp-content/uploads/2022/11/Replace-all-the-occurrence-of-a-pattern-in-a-line-1024x218.webp)
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 the "line" string with "sentences" on the second line](https://linuxtldr.com/wp-content/uploads/2022/11/Replacing-the-line-string-with-sentences-on-second-line-1024x220.webp)
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 all the "line" strings with "sentences" on the second line](https://linuxtldr.com/wp-content/uploads/2022/11/Replacing-all-the-line-string-with-sentences-on-second-line-1024x228.webp)
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 specified string on the range of lines](https://linuxtldr.com/wp-content/uploads/2022/11/Replacing-the-specified-string-on-the-range-of-lines-1024x222.webp)
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:
![Replacing the last line with the specified string](https://linuxtldr.com/wp-content/uploads/2022/11/Replacing-the-last-line-with-specified-string-1024x214.webp)
Duplicate the Replaced Line
The following command will duplicate the replaced line with a new line.
$ sed 's/line/sentences/p' file.txt
Output:
![Duplicate the replaced lines with new line](https://linuxtldr.com/wp-content/uploads/2022/11/Duplicate-the-replaced-lines-with-new-line-1024x311.webp)
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:
![Only output the replaced lines](https://linuxtldr.com/wp-content/uploads/2022/11/Only-output-the-replaced-lines-1024x172.webp)
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.
![Delete the second line from the referenced file](https://linuxtldr.com/wp-content/uploads/2022/11/Delete-the-second-line-from-the-referenced-file-1024x212.webp)
Replace the “2d
” with “$d
” to delete the last line.
$ sed '$d' file.txt
Output:
![Delete the last line from the referenced file](https://linuxtldr.com/wp-content/uploads/2022/11/Delete-the-last-line-from-the-referenced-file-1024x219.webp)
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.
![Delete the range of lines from the referenced file](https://linuxtldr.com/wp-content/uploads/2022/11/Delete-the-range-of-lines-from-the-referenced-file-1024x187.webp)
To delete all the lines after a second line, use the following command.
$ sed '2,$d' file.txt
Output:
![Remove all the lines after the second line](https://linuxtldr.com/wp-content/uploads/2022/11/Remove-all-the-lines-after-the-second-line-1024x119.webp)
The following command will remove the lines matching the “line” string.
$ sed '/line/d' file.txt
Output:
![Remove all the lines with matching strings from the referenced file](https://linuxtldr.com/wp-content/uploads/2022/11/Remove-all-the-lines-with-matching-string-from-the-referenced-file-1024x163.webp)
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