problem with sed while replacing a string with another

Hi,
I have a line something like this

sys,systematic,system
I want to replace only the word system with HI

I used sed for this as below
echo sys,systematic,system | sed 's/system/HI/'

but I got output as
sys,HIatic,system

I wanted output as
sys,systematic,HI

Please tell me how can I achieve desired output?

Thanks
friendyboy

If file is consistent, this might work

sed 's/system/HI/2' file
sed -e 's/^system,/HI,/g' -e 's/,system$/,HI/g' -e 's/^system$/HI/g' -e 's/,system,/,HI,/g'  file.txt

irrespective of "system" position the above code will work.

Thanks panyam,
I was looking for similar one irrespective of the position of word "system"
I have lots of lines with such set of words.Is there any shorter way of acheiving ?

Please suggest if any

Thanks
friendyboy

sed's expression set is limited. You can achieve better results with perl:

perl -p -e 's/\bsystem\b/HI/g'

The \b encoding before and after essentially tells perl to look at "word boundaries". You can achieve a similar effect in sed with [^a-zA-Z0-9-_] but you still have to take care of beginning and end-of-line situations. Perl's \b does this automatically.

The perl example is a good one.

If you must do it in sed, this one is a few characters shorter than the previous example :wink:

echo sys,systematic,system | sed -e 's/^/,/' -e 's/$/,/' -e 's/,system,/,HI,/g' -e 's/^,//' -e 's/,$//'

You could also do it in awk:

echo sys,systematic,system | awk 'BEGIN {FS=OFS=","} {for (i=1;i<=NF;i++) if ($i=="system") $i="HI"; print}'

Thanks canbridge.
Infact I cant use perl inside the script.

If I want the output as
sys,systematic,bigredhat\HI

where bigredhat is the system name obtained by uname -n.

what modifications need to be done in ur awk example?

Thanks
friendyboy

Do you mean like this?

echo sys,systematic,system | awk -v host=`uname -n` 'BEGIN {FS=OFS=","} {for (i=1;i<=NF;i++) if ($i=="system") $i=host "\\HI"; print}'

actually i wanted like

sys,systematic,begredhat\system

i.e uppend hostname\ befor the variable.

Thanks
friendyboy

thanks canbridge
I got what i wanted
i used $i=hosname"\\"$i

thanks a lot.I was really helped.

Thanks for all who have helped me on this.

Thanks
friendyboy