Top 15 Practical Examples of SED Linux Command [Updated 2024]
SED is a Linux command which is generally used to edit a file by replacing the string, delete strings and lines without even opening a file and without affecting the original content of the file. It is also called Stream Editor. It also allows complex pattern matching, which makes this command more powerful. This article will review top sed commands practical examples that help Linux users play with files without googling them.
Click Here for Tutorial for Beginners - Learn AWK Commands with Example
We have created a test file for performing various operations on it. Create a file testfile.txt by using the below commands:
#ping -c 10 192.168.0.102 > testfile.txt #cat testfile.txt
(1) To display version number
#sed --
version
(2) To display all usage of sed command (Help options)
#sed --
help
(3) Replace strings: sed command is used to replace strings in file. Below example replaces IP '192.168.0.102' with IP '98.34.65.34'.
#sed 's/192.168.0.102/98.34.65.34/' testfile.txt
Click Here for Complete List of Cyber Security Standards
(4) Replace multiple strings by one liner command: Below example replaces IP 192.168.0.102 with IP 98.34.65.34 and bytes with testbytes.
#sed 's/'192.168.0.102'/'98.34.65.34'/g; s/bytes/testbytes/g' testfile.txt
(5) Ignore case-sensitive while replacing strings: Use 'gi' instead of 'g' to ignore case sensitive in strings. Below example demonstrate usage of g and gi at different instants.
#echo 'This is Test File' | sed 's/this/change/gi'
(6) To print range of lines of text file: In below command, user can view content of only line number 3 to 7 of testfile.txt.
sed -n '3, 7p' testfile.txt
(7) To delete range of lines of text file: In below example, user can ignore the print of the content of line number 3 to 7 of testfile.txt.
sed '3, 7d' testfile.txt
Click Here for Linux cURL Commands: Quick Tutorial
(8) To print multiple range of lines of text file: By using below command, only line numbers 3,4,6,7 print as an output.
sed -n -e '3,4p' -e '6,7p' testfile.txt
(9) To replace string on a specific line number: In below example, line number 2 replaces IP 192.168.0.102 by IP 44.44.44.44 in output.
#sed '2 s/'192.168.0.102'/'44.44.44.44'/' testfile.txt
(10) Replace string on specific range of lines: In below example, line number 2 to 4 replaces IP 192.168.0.102 by IP 44.44.44.44 while print on terminal.
sed '2,4 s/'192.168.0.102'/'44.44.44.44'/' testfile.txt
(11) To delete a particular line in test file: Below command deletes the 4th line of testfile.txt while printing.
#sed '4d' testfile.txt
(12) To delete lines of particular range in a test file: Below Command delete line numbers 4 to 9 while print output.
#sed '4,9d' testfile.txt
(13) To delete nth to last line while print content of test file: Below command delete whole text after 4th line while print content of testfile.txt
Click Here for SSH Protocol: Quick Explanation
(14) To delete a line by pattern matching string: Below command delete all lines containing IP 192.168.0.102 while print output on the terminal.
#sed '/'192.168.0.102'/d' testfile.txt
(15) To insert blank lines in test file: Below command is used to insert blank lines in testfile.txt while print on terminal.
#sed G testfile.txt
Conclusion
In this article, we have covered sed command examples and usage. You can be more innovative by constructing commands to achieve the desired results.
Subscribe us to receive more such articles updates in your email.
If you have any questions, feel free to ask in the comments section below. Nothing gives me greater joy than helping my readers!
Disclaimer: This tutorial is for educational purpose only. Individual is solely responsible for any illegal act.
I think this site holds very fantastic written content material articles.