20 Linux Sed Command Examples

By | January 26, 2017

Linux Sed is stream(buffer) editor.It means it receives data from stdin and process the data and output it to Desktop or some file.Sed process the data in pipe.So,we dont need to give input from file.Any intermediate output can be given to sed pipe command.

In simple words,Sed process the file/buffer in pipe.It is used to get portion of text from file (or) replace portion of text with another text.One need to have this usage in handy to work like Pro.

20 Linux Sed Command Examples

20 Linux Sed Command Examples

Let’s go by Examples one after another.

Assume we have text file with below content.

[ SLTMachine: ] # cat myFile.txt

 SimplyLearnTopics.com is blog on the C,C++,Data Structures and much more
 logical Queries.It was started by Srinivas Daripelli.He has experience
 Blog on c programs
 Blog oc cpp programs
 Blog on java programs
 Blog oc dot net programs

Usage 1: How to replace String

[ SLTMachine: ] # sed 's/cpp/linux/' myFile.txt
 SimplyLearnTopics.com is blog on the C,C++,Data Structures and much more
 logical Queries.It was started by Srinivas Daripelli.He has experience of 15 years in programming.
 Blog on c programs
 Blog oc linux programs
 Blog on java programs
 Blog oc dot net programs

If you observe the above output,i have replaced “cpp” by “linux”

Usage 2:How to print range of lines from file

[ SLTMachine: ] # sed -n '2,4p' myFile.txt
 logical Queries.It was started by Srinivas Daripelli.He has experience
 Blog on c programs
 Blog oc cpp programs

It will print the lines from 2nd to 4th.

Usage 3:How to replace nth repeated word with new word.

Say i want to replace 2nd repeated word of “programs”

[ SLTMachine: ] # sed 's/programs/code/2' myFile.txt

SimplyLearnTopics.com is blog on the C,C++,Data Structures and much more
 logical Queries.It was started by Srinivas Daripelli.He has experience of 15 years in programming.
 Blog on c programs
 Blog oc linux code
 so on..

Usage 4: How to print complete file except first 1 line from 2nd line (or given number of lines).

[ SLTMachine: ] # sed '2,1d' myFile.txt
 SimplyLearnTopics.com is blog on the C,C++,Data Structures and much more
 Blog on c programs
 Blog oc cpp programs
 Blog on java programs
 Blog oc dot net programs

The above command skips one line from 2nd line.that is two lines.

Usage 5:How to replace all the occurrences of word in file.

[ SLTMachine: ] # sed 's/programs/coding/g' myFile.txt
 SimplyLearnTopics.com is blog on the C,C++,Data Structures and much more
 logical Queries.It was started by Srinivas Daripelli.He has experience
 Blog on c coding
 Blog oc cpp coding
 Blog on java coding
 Blog oc dot net coding

Usage 6:Print two sections of text file.How to print 1-2 lines and 4 and 5 lines.

[ SLTMachine: ] # sed -n -e '1,2p' -e '4,5p' myFile.txt
 SimplyLearnTopics.com is blog on the C,C++,Data Structures and much more
 logical Queries.It was started by Srinivas Daripelli.He has experience
 Blog oc cpp programs
 Blog on java programs

Usage 7: Edit the source file by using sed.Normally,sed will not modify the source file.

[ SLTMachine: ] # sed -i '1d'  myFile.txt

Above command will remove the first line from myFile.txt

Usage 8: Take backup of source file before we edit the file.

[ SLTMachine: ] # sed -i.bak '1d'  myFile.txt

as a practice,we take the backup of a file ,before we edit the file.In the same way.with the option “-i.bak”,it will

provide the backup of ile.

[ SLTMachine: ] # ls -l myFile*
 -rw-r--r-- 1 root root 231 Jan 26 12:30 myFile.txt
 -rw-r--r-- 
1 root root 202 Jan 26 12:54 myFile.txt.bak

Usage 9: how to delete first 3 lines from file myFile.txt

[ SLTMachine: ] # cat /tmp/passwd | sed ‘1,3d’

Usage 10: How to delete empty lines from file myFile.txt

[ SLTMachine: ] # sed  '/^$/d'  myFile.txt--

Usage 11: How to delete empty lines Or starting with ‘#’ from file myFile.txt

[ SLTMachine: ] # sed '/^#\|^$\| *#/d' myFile.txt

It will remove the lines ,which starts with “#”.Mostly we use # to add comment section in every programming language.

Usage 12: How to Add new line after your match.

[ SLTMachine: ] # sed '/programs/ a "new line - simplylearntopics.com"' myFile.txt
 SimplyLearnTopics.com is blog on the C,C++,Data Structures and much more
 logical Queries.It was started by Srinivas Daripelli.He has experience
 Blog on c programs
 "new line - simplylearntopics.com"
 Blog oc cpp programs
 "new line - simplylearntopics.com"
 Blog on java programs
 "new line - simplylearntopics.com"
 Blog oc dot net programs
 "new line - simplylearntopics.com"

With the above command,i will add new line after match wherever i find match of “program”

Usage 13: How to Add new line before your match.

[ SLTMachine: ] ~ # sed '/programs/ i "new line - simplylearntopics.com"' myFile.txt
 SimplyLearnTopics.com is blog on the C,C++,Data Structures and much more
 logical Queries.It was started by Srinivas Daripelli.He has experience
 "new line - simplylearntopics.com"
 Blog on c programs
 "new line - simplylearntopics.com"
 Blog oc cpp programs
 "new line - simplylearntopics.com"
 Blog on java programs
 "new line - simplylearntopics.com"
 Blog oc dot net programs

With the above command,i will add new line before the match wherever i find match of “program”

Usage 14: convert lowercase letters to upper case letters

[ SLTMachine: ]~ # echo "acbd" | sed 'y/abc/ABC/'
 ACBd

with the option ‘y’,we can convert “lower case” letters to “Upper case” Letters.but we have to mention the equivalent in the syntax itself.

Usage 15: Insert Spaces in the file.

[ SLTMachine: ]~ # sed G myFile.txt
 SimplyLearnTopics.com is blog on the C,C++,Data Structures and much more
 logical Queries.It was started by Srinivas Daripelli.He has experience
 Blog on c programs
 Blog oc cpp programs
 Blog on java programs
 Blog oc dot net programs

“G” will add the new line for non empty lines in the file.

Usage 16: Display usernames avilable in “/etc/paswd” file.It containes linux system user id’s information.

[ SLTMachine: ]~ # sed 's/\([^:]*\).*/\1/' /etc/passwd
 root
 bin
 daemon
 lp
 mail
 games

Usage 17: Delete any duplicates,if they are consecutive in a File.

i have added few duplicates,please check my input file – myFile.txt

[ SLTMachine: ]~ # cat myFile.txt
SimplyLearnTopics.com is blog on the C,C++,Data Structures and much more
logical Queries.It was started by Srinivas Daripelli.He has experience
Blog on c programs
Blog on c programs
Blog on c programs
Blog on c programs
Blog oc cpp programs
Blog on java programs
Blog on c programs
Blog oc dot net programs
Blog on c programs


[ SLTMachine: ]~ # sed '$!N; /^\(.*\)\n\1$/!P; D' myFile.txt
SimplyLearnTopics.com is blog on the C,C++,Data Structures and much more
logical Queries.It was started by Srinivas Daripelli.He has experience
Blog on c programs
Blog oc cpp programs
Blog on java programs
Blog on c programs
Blog oc dot net programs
Blog on c programs

If you observe the above output,it will remove the duplicates,if they are consecutive only.

Usage 18: Print Exact 3rd or nth line from myFile.txt.

[ SLTMachine: ]~ # sed -n '3'p myFile.txt

Usage 19: Print Last line from myFile.txt.

[ SLTMachine: ]~ #sed -n '$'p myFile.txt

Usage 20: Can we replace two words at a time ? Yes we can.

[ SLTMachine:]~ # sed -i 's/oldjava/newjava/gi;s/oldphone/newphone/gi' myfile.txt

the above command replaces s the word from “oldjava” to “newjava”

and “oldphone” to “newphone”

Thank you for reading the 20 Linux Sed Command Examples.If you know,any one new please
post it in the comments section.I will add them as bonus tips here.

Happy SimplyLearning 🙂

The following two tabs change content below.

SRINIVAS DARIPELLI

Myself SRINIVAS DARIPELLI. I have 15+ Years of Experience in Programming worked on multiple technologies.Apart from it,I am a blogger, writer, editor, artist and dad 🙂 .I believe in reality.I love to share the Helpful things around the Technology. Feel free to connect with me

Leave a Reply