sed command in UNIX
sed (Stream Editor):
To convert/replace the character or string , to print the
expected lines in the output and more :
Let's create a file
named fileName1 with below contents :
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
This is my town.
sed -e 's/This/That/g' fileName1 --- converts all the "This"
to That from the file fileName1.
or
sed -e 's/This/That/' fileName1
--- This will convert first
occurance only from each line.
Output:
That fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
That is my town.
echo This This
This This | sed -e 's/This/That/' ---- This command will replace the first
occurance of the word "This" from the line , if more than one line
exits then 1st occurance from each line.
Output:
That This This This
echo This This
This This | sed -e 's/This/That/g' ---- This command will replace all "This" , "g" stands for "global"
Output:
That That That That
echo This This
This This | sed -e 's/This/That/2' ---- This command will replace the second
occurance of the word "This" from
the line , if more than one line exits then 2nd occurance from each line.
Output:
This That This This
Let's take the file :
fileName1:
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
This is my town.
sed -e 's/s/S/' fileName1
--- replaces all the first occurance of "s" from each line to
"S"
Output:
ThiS fruit is Red.
Sunday is weekend.
fruit and vegetable are healthy.
We have work on week dayS.
ThiS is my town.
sed -e 's/e/E/g'
fileName1 ---- replaces all "e" to "E"
This fruit is REd.
sunday is wEEkEnd.
fruit and vEgEtablE arE hEalthy.
WE havE work on wEEk days.
This is my town.
echo We are enjoying the weekend. | sed -e 's/e/E/5g'
--- make changes from 5th
occurance from the line , if more
than one line exits then from 5th occurance in each line.
Output:
We are enjoying the wEEkEnd.
Divert the sed output to a file , if the output file
exits, it appends. If the output file does not exit, then it creates.
echo Seven days in a week | sed 's/Seven/7/g' > sedFile.txt
No output will be displayed.
Lets open the file
sedFile.txt :
7 days in a week
sed '4 s/e/E/' fileName1
--- replace "e" with
"E" , first occurance on 4th line only.
Output:
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
WE have work on week days.
This is my town.
sed
'4 s/e/E/g' fileName1
--- replace all "e"
with "E" on 4th line only.
Output:
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
WE havE work on wEEk days.
This is my town.
sed '2 , 4 s/e/EE/g' fileName1
--- replace "e" with
"EE" from lines 2, 3, 4 .
This fruit is Red.
sunday is wEEEEkEEnd.
fruit and vEEgEEtablEE arEE hEEalthy.
WEE havEE work on wEEEEk days.
This is my town.
sed
'3 , $ s/h/HH/g'
fileName1 --- --- replace
"h" with "HH" from
line 3 to last line.
This fruit is Red.
sunday is weekend.
fruit and vegetable are HHealtHHy.
We HHave work on week days.
THHis is my town.
sed
-n 3p fileName1
--- prints only 3rd line.
fruit and vegetable are healthy.
sed
-n 2,4p fileName1
--- prints lines 2 to 4, ie, 2, 3, 4
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
sed
-n '2,$p' fileName1
--- prints lines 2 to last.
sed
4p fileName1 ---
prints line 4 twice
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
We have work on week days.
This is my town.
sed -e 's/week/WWWW/p' fileName1
--- prints the affected lines twice.
This fruit is Red.
sunday is WWWWend.
sunday is WWWWend.
fruit and vegetable are healthy.
We have work on WWWW days.
We have work on WWWW days.
This is my town.
sed -n 's/h/H/p'
fileName1 --- prints affected
lines only.
THis fruit is Red.
fruit and vegetable are Healthy.
We Have work on week days.
THis is my town.
sed
-i 's/t/TTT/ ' --- when used "i"
like this , the sed command makes
changes inside the file , does not print the
output.
sed -i 's/t/TTT/' fileName1
--- does not show the output, let's see inside the file :
cat fileName1
This fruiTTT is Red.
sunday is weekend.
fruiTTT and vegetable are healthy.
We have work on week days.
This is my TTTown.
sed
-e 's/t/WW/gi' fileName1
--- using "i" like
this, you can apply the expected change
irrespective of the case , "i" removes the case sensitivity in this case.
WWhis fruiWW is Red.
sunday is weekend.
fruiWW and vegeWWable are healWWhy.
We have work on week days.
WWhis is my WWown.
sed
3d fileName1 --- deletes
3rd line from the
output not from the file.
This fruit is Red.
sunday is weekend.
We have work on week days.
This is my town.
sed
$d fileName1 ---
deletes last line
from the output not from the file.
sed
'3,$d' fileName1 ---
deletes 3rd to last line
from the output not from the file.
Let's update fileName1 , blank/empty
lines have been added as below :
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
This is my town.
sed
's/^$/**Empty Line**/g' fileName1
--- inserts the given text in the
empty lines.
This fruit is Red.
sunday is weekend.
**Empty Line**
fruit and vegetable are healthy.
**Empty Line**
We have work on week days.
This is my town.
Let's create a new
file : fileName2 with below contents:
This this Theese thys this
thes that ssys this gum iss ssyy
ssa thya syha this this ibm this shss sbss
sed -e 's/this//g'
fileName2 ---
deletes the given word in the output
This Theese thys
thes that ssys gum
iss ssyy
ssa thya syha
ibm shss sbss
No comments:
Post a Comment