Replace fixed strings only

Hi All,

I just need to do find and replace in a file....
say for eg I have the input file like below:
in.txt
#####
oldtextoldtext
oldtext
oldtext
oldtext
oldtext123
oldtext-
oldtext

I need to replace oldtext to newtext... my output file should come like below..
out.txt
######
oldtextoldtext
newtext
newtext
newtext
oldtext123
oldtext-
newtext

Pls help me....
Thanks in advance
Saravana

what have you tried? put in some effort.

Did you try anything?

I tried with this cmd :
sed -e 's/oldtext/newtext/g' $file

but it is repalcing all the lines... :frowning:

sed 's/^oldtext$/newtext/g' $file

Sorry... I forgot add this line in my input file.... If I have the below line in my file then it will not work....

something something oldtext

So it will not work.....

there's no need for 'g' as the pattern will appear only ONCE.

sed -e 's/^oldtext$/newtext/' -e 's/ oldtext / newtext /g' -e 's/ oldtext$/ newtext/'  -e 's/^oldtext /newtext /'  input_file.txt

(right about the unneeded g)

sed 's/\<oldtext\>/newtext/g'
(not all sed's support this)

almost does what you want except for the "oldtext-" line, since '-' is a word boundary too

This substitues all occurances of oldtext that are surrounded either by blanks or by start/end of the line.

sed 's/\(^\|[[:blank:]]\)oldtext\([[:blank:]]\|$\)/\1newtext\2/' filename