Top 15 Practical Examples of SED Linux Command

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 file. It is also called Stream Editor. It also allows complex pattern matching which makes this command more powerful.  In this article, we will review top sed commands practical examples which helps Linux users to play with files without googling it.

We have created testfile for performing various operations on it. Create a file testfile.txt by using below commands:

#ping -c 10 > 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 ‘’ with IP ‘’.

#sed 's/' testfile.txt


(4) Replace multiple strings by one liner command: Below example replaces IP with IP and bytes with testbytes.

#sed 's/''/''/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


(8) To print multiple range of lines of text file: By using below command, only line numbers 3,4,6,7 print as a 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 by IP in output.

#sed '2 s/''/''/' testfile.txt


(10) Replace string on specific range of lines: In below example, line number 2 to 4 replaces IP by IP while print on terminal.

sed '2,4 s/''/''/' testfile.txt


(11) To delete a particular line in test file: Below command deletes 4th line of testfile.txt while print.

#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


(14) To delete line by pattern matching string: Below command delete all lines containing IP while print output on terminal.

#sed '/''/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



In this article, we have covered practical examples of sed command. You can be more innovative by constructing commands to achieve 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.


Your email address will not be published. Required fields are marked *