sed (stream editor)  
 
 
 
After reading this article , a reader should know about the 
following terms. 
● About sed  
● Syntax of sed. 
● How sed works 
● How to make a sed script. 
 
Prerequisite  
● A user should aware and basic idea about sed command. 
 
 
 
 
 
 
 
Author Bipul kumar 
BIPUL.NET 
bipul.opensource[AT]gmail.com 
1.  What is sed ? 
 
 
sed is a stream editor. A stream editor is used to perform basic text transformations on 
an input stream. Here input stream is a file or input from a pipeline. 
sed was developed from 1973 to 1974 by​ ​Lee E. McMahon​ at Bell Labs. It’s 
Implementation language is in C and Influenced by ed command. It is generally used in 
Scripting language. 
 
 
 
 
2. Syntax of sed. 
The main syntax of sed  
sed SCRIPT INPUTFILE 
 
But when you are using sed on command line for single editing. We uses following. 
sed  s/PATTERN/SUBSTITUTION/ 
 
Eg . echo "Unix is the best OS System" | sed s'/Unix/Linux/' 
Here Unix is Pattern, which is get replaced by Linux. 
 
File­based sed scripts 
sed ­i ­f command.sed FileNeedToChange 
­i option allow permanent changes in File(FileNeedToChange) 
­f option allow to add the contents of script­file (command.sed) to be executed 
 
 
 
 
 
 
 
 
 
 
 
 
3. How sed works 
Let’s illustrate the following working diagram. 
 
 
 
Loop through all lines 
For each command 
If line qualifies for that command or matches the pattern 
Apply command to the line 
Write results to stdout 
Endif 
end for 
endloop 
 
sed maintain two data buffer: 
1. Active pattern space i.e HOLD SPACE Buffer 
2. Auxiliary hold space i.e PATTERN SPACE Buffer 
 
When sed script 1 executed, then sed reads one­line from input string file(2). 
Remove any trailing newline, and place it in the pattern space(4,2). 
Then command are executed, but before the command executed, it must be verified the 
condition. Here condition is address where ​command specific​ is associated with it at 
(2,4,5). Then last Output. 
● Each line of a input file(3) is copied into pattern space(2,4) 
● All editing commands are applied in order to each line of input.. 
 
Commands specific​ in sed. 
H   ​Append pattern space to hold space. 
h  ​Copy pattern space to hold space. 
G​ Append contents of hold space to pattern space. 
g ​ Copy contents of hold space to pattern space. 
x​  Exchange, Swap contents of hold space and pattern space. 
d ​Delete the pattern space. 
N ​Next line to pattern space. 
p ​Print the pattern space to standard output. 
S ​Search and replace. 
 
 
Let’s have a example where we swap the position word starting with ​W​ and ​F  
File name:  word.list 
Wise 
Fool 
Wisecon 
Flower 
West 
Fire 
 
 
 
 
Sed script File name:  ​commands 
/W/{ 
H 
d 
} 
/F/{ 
G 
} 
 
Now run the sed script on file word.list in your bash shell. 
#sed ­f commands word.list 
 
 
 
 
 
4. How to make a sed script. 
To make a sed script simply we need to make below file and give x(executable permission) 
File name commands: 
#!/bin/sed ­f 
/W/{ 
H 
d 
} 
/F/{ 
G 
} 
Then​ ​#chmod +x commands ; ./command word.list 
Source 
https://en.wikipedia.org/wiki/Sed 
s!   Aurelio Jargas    !​ ​http://aurelio.net/sed/​                       ! 
s!     Eric Pement     !​ ​http://www.pement.org/sed/​ ! 
s!   Laurent Le Brun   !​ ​http://laurent.le­brun.eu/geek​                ! 
s!    Paolo Bonzini    !​ ​http://sed.sf.net/grabbag/​                    ! 
s! Tilmann Bitterberg  !​ ​http://www.bitterberg.de/tilmann/sed­en.html​  ! 
s!    Yao­Jen Chang    ! 
http://web.archive.org/web/20141115192529/http://main.rtfiber.com.tw/~changyj/sed/​ ! 
s! Yiorgos Adamopoulos !​ ​http://www.dbnet.ece.ntua.gr/~george/sed/OLD/​ ! 
 
 
 
 
 
 
 
 

sed(1)