Hello, everyone In this Article we are going to learn how we can use the sed command in Linux with examples in a very simple and easy step-by-step approach.
With the help of sed command we can do Following tasks
- Print the Line Number
- Find and replace the text
- Add the Text before the line
- Add the Text After the line
How to Print the Line Number
How to Print the Line using sed command
To print the Specific line of the file using sed command then we can add (-n) option with the line number that we want to print
# sed -n '1p' /sample | cat -n
How to Pint the line number with the specific range
To print the specific Lines on the file then we can add the range for the line that we want to print in the sed command
# sed -n '10,15p' /sample | cat -n
How to print the specific lines from the file
To print the specific lines from the file then we can add the ; symbol with the line number that we want to print
# sed -n '1p;5p' /sample | cat -n
How to print the last line
To print the last line of the file and we don’t know the number then we can use( $ ) symbol with( p ) option
# sed -n '$p' /sample | cat -n
How to specify a different range
We can also specify the different range in sed command First Here we want to print (1-5) Line number then (11-15) Line number and we want to print 21 line number
# sed '1,5p;11,15p,21p' /sample | cat -n
How we can Find and replace the text in the file
How we can find and replace the text in the file
Suppose that we want to find and replace the root text from the file then we can add the ( -s )option in the sed command.
We can perform this operation with 2 ways
1 – If we just want to find and replace the text on the file and to display the output on the screen this will not effect on the original file
To do the above task we can execute the following command in this command we are replacing the root keyword with the network keyword and g means that replace globally on everywhere the root keyword is coming
# sed 's/root/netowork/g' /sample | cat -n
How to change in specific line number
To change in the specific line number then we can add the line number in the sed command
# sed '1s/root/network/g' /sample | cat -n
How to change in the specific range
To change in the specific range of the number then we can add the range in the sed command. Here we are replacing in the 1-15 line number
# sed '1,15s/root/network/g' /sample
How to ignore case sensitiveT
To ignore the case sensitive of the word then we can use i option in the sed command, Here if we specify the ROOT word in the Capital letter then this will ignore the case sensitive, and still it replaces the word.
# sed 's/ROOT/network/gi' /sample
How to replace multiple keywords
To replace root keyword with network keyword and shutdown keyword with cloud keyword same time then we can add ; symbol in sed command.
# sed 's/root/netowrk/g;s/shutdown/cloud/g' | cat /sample
2 – If we just want to find and replace the text on the file and that effect the original file then we can add the ( -i ) option in sed command this will find and replace the word in the original file as well
# sed -i 's/root/network/g' /sample
How to replace the symbol
To replace the symbol in the file then we can also do this using the following command here we are searching the ( : ) symbol and replacing it with the ( >> ) symbol
# sed -i 's/:/>>/g' /sample
How to comment on the specific line
To comment on the specific line number then we can execute the following command.
Here we are commenting the 5th line number
# sed '5s/^/#/' /sample
How to uncomment the Line
To uncomment the line then we can execute the following command
# sed '5s/^#//' /sample
How to Delete the Line in the File
How to delete the line on the screen
To delete the line in the file on the screen then we can add the ( d ) option with the line number in the sed command Here we are deleting the 5th line of the file
# sed '5d' | cat -n
How to delete the Specific file range
To delete the file number with the specific range then we can define the range in the sed command
# cat -n /sample | sed '5,7d'
How to delete the line of the file in the original file
To delete from the original file then we can add ( i ) option in the sed command
# sed -i '5,7d' /sample
How to add the text before the line number
To add the text before the line number then we can add ( i ) option in sed command here we are inserting devopstutbefore before the 5th line number
# sed '5i devopstutbefore ' /sample
How to add the text after the line number
To add the text after the line number then we can add ( a ) option in the sed command here we are inserting devopstutafter after the 7th line number
# sed '7a devopstutafter ' /sample
Conclusion
These are the most common usage of sed command .
If you want to learn Linux in the most simple language then please click on this link.
If you want to see the man page of sed command online then click on this link
This is the complete knowledge of how we can view the content of a file using the sed command in the Linux kernel. if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on the DevOps Articles