INTRODUCTION
SED : stands for stream editor used for Filtering and Transforming on input stream from Files or pipelines
HOW TO INSTALL SED :
- Debian
sudo apt-get install -y sed
- RedHat | CentOS
yum install sed -y
HOW TO USE SED WITH EXAMPLES
- You can use it to print lines from text file
sed '3,1p' head.txt
![](http://unixcop.com/wp-content/uploads/2021/10/CAT_HEAD.jpg)
![](http://unixcop.com/wp-content/uploads/2021/10/SED_Line-3-.jpg)
HINT :
Here will print the content of text file with the highlighted line number 3 only
You can also print range of lines for example from line 1 to 3
sed '1,3p' head.txt
![](http://unixcop.com/wp-content/uploads/2021/10/SED_RANGE-13.jpg)
- You Can also print selected lines only without all lines exist on text file by using argument -n
sed -n '1,3p' head.txt
![](http://unixcop.com/wp-content/uploads/2021/10/New_SED.jpg)
- You can also print lines with specific streams or start with specific stream
sed -n '/unixcop/p' head.txt
![](http://unixcop.com/wp-content/uploads/2021/10/SED_UNIX.png)
sed -n '/Mostafa/p' head.txt
![](http://unixcop.com/wp-content/uploads/2021/10/SED_Mostafa.png)
sed -n '/^Bye/p' head.txt
![](http://unixcop.com/wp-content/uploads/2021/10/CAT_SED_ADD.jpg)
![](http://unixcop.com/wp-content/uploads/2021/10/SED_START.jpg)
- You Can Use it to replace some stream with another stream
sed 's/Mostafa/UNIXCOP/' head.txt
![](http://unixcop.com/wp-content/uploads/2021/10/SED_REPLACE.jpg)
- You can also append some streams to the lines
sed 's/unixcop/ & Mostafa and Omar and Ahmed/' head.txt
![](http://unixcop.com/wp-content/uploads/2021/10/SED_APPENED.jpg)
- You can use it to delete range of lines or streams
sed '3,4d' head.txt
![](http://unixcop.com/wp-content/uploads/2021/10/SED_DEL.jpg)
sed '/^Bye/d' head.txt
![](http://unixcop.com/wp-content/uploads/2021/10/SED_DELE.jpg)
- You can use sed to read sed command from text file with .sed extension
HINT :
You must write the sed command with the following form shown in command.sed file
I used to delete the line that starts with Bye word
sed -f command.sed head.txt
![](http://unixcop.com/wp-content/uploads/2021/10/SED_SCRIBT.jpg)
CONCLUSION
This article clarify the usages of sed editor with examples .. For more info about sed use the following ” man sed ”