SED Command Tutorial in Linux



Introduction:

SED command is very powerful tool in Linux and can be used vastly in shell scripting. It is called as stream editor.
It can perform lot of function on a file like replacing,finding,insertion and deletion.It is capable of solving complex text processing tasks with few lines of code.


Syntax:



sed OPTIONS... [SCRIPT] [INPUTFILE...] 




Examples:

Lets create a sample file with some context.

cat DEMO.txt


Example 1) Replace a pattern in a file. It will replace all occurrence of the pattern.
I the demo file I am going to replace the word "We" with "I". Please note it don't save the changes it is just show on the screen.

himanshu@himanshu-ThinkPad-T430 ~ $ sed 's/We/I/g' DEMO.txt 
I are an IT Services and Training provider. I are the believers in quality services and support.
I develop, support and maintenance services across the globe. 
I have been dealing with customers across the globe and deliver quality solutions and training to them. 
I practice and believe in Green IT initiatives, improve and optimize customer’sbusiness processes.


Example 2)  Replace an exact word in a file.

We use \b for boundation of exact word. I am replacing the word "Green" with "Black"


himanshu@himanshu-ThinkPad-T430 ~ $ sed 's/\bGreen\b/Black/g' DEMO.txt 
We are an IT Services and Training provider. We are the believers in quality services and support.
We develop, support and maintenance services across the globe. 
We have been dealing with customers across the globe and deliver quality solutions and training to them. 
We practice and believe in Black IT initiatives, improve and optimize customer’sbusiness processes.

Example 3)  Replace a pattern which comes in starting of a line.



I am going to replace "We" to "I" but only where it comes at beginning of line.


himanshu@himanshu-ThinkPad-T430 ~ $ sed 's/^We/I/g' DEMO.txt 
I are an IT Services and Training provider. We are the believers in quality services and support.
I develop, support and maintenance services across the globe. 
I have been dealing with customers across the globe and deliver quality solutions and training to them. 
I practice and believe in Green IT initiatives, improve and optimize customer’sbusiness processes.


Example 4) Replace a pattern which comes at end of line.


I am replacing "support" with "fun" but only present at end of line. Please also note that the pattern is "support."



himanshu@himanshu-ThinkPad-T430 ~ $ sed 's/support.$/fun/g' DEMO.txt 
We are an IT Services and Training provider. We are the believers in quality services and fun
We develop, support and maintenance services across the globe. 
We have been dealing with customers across the globe and deliver quality solutions and training to them. 
We practice and believe in Green IT initiatives, improve and optimize customer’sbusiness processes.

Example 5)  Delete any line that contains a pattern.


I am deleting lines which contains "support" .

himanshu@himanshu-ThinkPad-T430 ~ $ sed '/support/d' DEMO.txt 
We have been dealing with customers across the globe and deliver quality solutions and training to them. 
We practice and believe in Green IT initiatives, improve and optimize customer’sbusiness processes.




Example 6)  If we want to make changes directly to file permanently.

I want to permanently replace "We" to I.


himanshu@himanshu-ThinkPad-T430 ~ $ sed -i 's/We/I/g' DEMO.txt
himanshu@himanshu-ThinkPad-T430 ~ $ cat DEMO.txt
I are an IT Services and Training provider. I are the believers in quality services and support.
I develop, support and maintenance services across the globe. 
I have been dealing with customers across the globe and deliver quality solutions and training to them. 
I practice and believe in Green IT initiatives, improve and optimize customer’sbusiness processes.