The `sed`
command is an essential tool for manipulating text in Linux. It allows you to search for patterns in a text and perform various operations on the matching text, such as replacing it, deleting it, or printing it. This command takes input from a file or standard input.
The default sed command doesn’t make changes to the original file until you used the -i
command line parameter. The command alerts the text and sends the result to standard output. You can also redirect the text to the file as per the requirements.
In this article, we’ll provide 15 practical examples of using the `sed`
command to perform various tasks.
Replace Strings Example using Sed
1. Replace string in a file
To replace a string in a file, you can use the 's/old_string/new_string/g'
command. The syntax is:
1 2 | # Syntax sed 's/old_string/new_string/g' file_name |
To replace strings in a file using the sed command, use the -i
argument to make the changes in-place. The command string must include the substitute command ‘s’, the original string, and the replacement string. For example, to replace all instances of the string “apple” with the string “banana” in a file called fruits.txt, use the following command:
See the following example:
sed 's/apple/banana/g' fruits.txt
This command reads the content from the “fruits.txt” file and replaces all occurrences of the word “apple” with the word “banana” and prints the resulting text to the terminal. The g
tells the command to replace all matching occurrences globally in the file.
You can also make the changes in the same file with the -i
option.
sed -i 's/apple/banana/g' fruits.txt
You will see that the original file is modified. You can also make s backup of file before making changes in original file.
sed -i.bak 's/apple/banana/g' fruits.txt
A backup file of the original will be created in current directory with name fruits.txt.bak.
2. Replace the first occurrence of each line
To substitute only the first occurrence of a pattern on each line, you can use the s/pattern/replacement/ command. For example, to replace only the first occurrence of the word “apple” with the word “banana” in the file fruits.txt, you can use the following command:
sed 's/apple/banana/' fruits.txt
3. Replace the last occurrence of each line
To substitute only the last occurrence of a pattern on each line, you can use the ‘s/pattern/replacement/g’ command. For example, to replace only the last occurrence of the word “apple” with the word “banana” in the file fruits.txt`, you can use the following command:
sed 's/\(.*\)apple/\1banana/g' fruits.txt
4. Replace string at specific line number
To replace a string on a specific line, you can use the lineNumbers/pattern/replacement/ command. For example, to replace the first occurrence of the word “apple” with the word “banana” on line 3 of the file fruits.txt, you can use the following command:
sed '3s/apple/banana/' fruits.txt
5. Replace string from range of line numbers
To replace a string on a range of lines, you can use the startLineNumber,endLineNumber/pattern/replacement/ command. For example, to replace the first occurrence of the word “apple” with the word “banana” on lines 3 through 5 of the file fruits.txt, you can use the following command:
sed '3,5s/apple/banana/' fruits.txt
Deleting Lines in File using Sed
6. Delete first line from file
To delete a line that contains a certain string, you can use the /pattern/d command. For example, to delete all lines that contain the word “apple” in the file fruits.txt, you can use the following command:
sed '1d' fruits.txt
7. Delete line contains certain string
To delete a line that contains a certain string, you can use the ‘/pattern/d’ command. For example, to delete all lines that contain the word “apple” in the file fruits.txt, you can use the following command:
sed '/apple/d' fruits.txt
8. Delete lines except the matching string
To invert the matching lines, you can use the `!`
operator in combination with other commands. For example, to delete all lines that contain the word “apple” in the file fruits.txt, you can use the following command:
sed '/apple/!d' fruits.txt
9. Delete the range of lines
To delete a range of lines, you can use the startLineNumber,endLineNumber
d
command. For example, to delete lines 3 through 5 of the file fruits.txt, you can use the following command:
sed '3,5d' fruits.txt
10. Delete empty lines from file
You can remove the empty lines from the file using '/^$/d'
option.
sed '/^$/d' fruits.txt
This command consider a line as empty, that doesn’t contain any character, even a single space.
Printing Lines from File using Sed
11. Print line numbers only
To print only the line numbers of matching lines, you can use the ‘/pattern/=’ command. For example, to print the line numbers of all lines that contain the word “apple” in the file fruits.txt, you can use the following command:
sed '/apple/=' fruits.txt
12. Print the range of file
To print a range of lines, you can use the ‘start,endp’ commands. For example, to print the first 10 lines of the file fruits.txt, you can use the following command:
sed -n '1,10p' fruits.txt
Inserting Lines to File using Sed
13. Insert a new line after matching pattern
To insert a line after a certain pattern, you can use the ‘/pattern/a\newline’ command. For example, to insert the line “grapes” after the line that contains the word “apple” in the file fruits.txt, you can use the following command:
sed '/apple/a\grapes' fruits.txt
14. Insert a new line before matching pattern
To insert a line before a certain pattern, you can use the ‘/pattern/i\newline’ command. For example, to insert the line “grapes” before the line that contains the word “apple” in the file fruits.txt, you can use the following command:
sed '/apple/i\grapes' fruits.txt
Other Tasks using Sed
15. Change Case of Characters
To change the case of a string, you can use the ‘y/old/new/’ command. For example, to change all lowercase letters to uppercase in the file fruits.txt, you can use the following command:
sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' fruits.txt
Conclusion
We hope these examples have been helpful in showing you the versatility of the sed command. Remember that sed is a very powerful tool and it’s important to test your commands carefully before using them on important files. If you have any questions or need further assistance, don’t hesitate to ask. With these practical examples under your belt, you’ll be well on your way to mastering the sed command and becoming a proficient Linux administrator.