Replace One String With Another String

i have a file call text and i what the user to enter any word he/she wants to replace with any word

i current have

cat text

echo "Please enter the word to be amended:"
read old
echo "Please enter a new word:"
read new

sed "s/$old/$new/g" $1 > text

echo "press ENTER to continue"
read key

what is not right

What is $1?
I guess you are using text as input and output , you can replace (if perl installed):

sed "s/$old/$new/g" $1 > text

with

perl -pi -e "s/$old/$new/g" text

the script looks fine.

What's happening that's wrong? consider using set -x to see what you are doing where. Of course, [ "$1" == "text" ] && you get a problem.

What if $old or $new contains a "/" char? You have to escape appropriately your variables to use them in sed regular expressions.

might help to understand what it is you really mean by "what is not right"? A better question would be .... After I run the script my text file is empty or it gives me such and such results and I am looking for .... or it does nothing .... help us understand what your real problem is

I see several different potential problems .....

how do you call this script? (it is expecting a file name ... but the "cat text" always displays the file "text" )

the $1 does not necessarly have to be the file text

if you were to call this script without a file name (or with the file name of text ... you would indeed end up with an empty "text" file.

here is what I would do

change all references of "text" to "$1"

change the sed command to

cat $1 | sed s/'$old'/'$new'/g > $1

The way you wrote the sed empties the text file first then tries to put the now empty file back into itself. The cat/sed combination will empty the text file after it has done the cat.

The following leaves an empty file.

cat $1 | sed s/'$old'/'$new'/g > $1
Need to direct to a tmp file and then move it back (is there a better way?)
 cat $1 | sed s/'$old'/'$new'/g > $1.$$tmp
 mv $1.$$tmp $1

I tried my script under ksh, bash, and sh ... all worked with the cat $1 | sed .... > $1 approach ....

Which shell were you using that resulted in an empty file?

Your approach will work, just curious as to the portability of the code if yours fails (on the questioned sed) and mine works.

Thanks

Using ksh .
Looks like the problem is the platform.
Tested using

cat /tmp/t.txt |sed "s/old/new/g">/tmp/t.txt; cat /tmp/t.txt

File is empty on AIX (IBM) and SunOS.
It worked on HPUX.

intersting .. must be more than platform ... because I tested it on AIX (5.3 TL05) and Kubunto (7.10) and it worked ...

AIX 5.3 (how do you distinguish further? Not seeing anything more in uname -a )

The TL comes from my SA ... sorry

If I remember correctly older versions of bash doesnt support reading a file and redirecting output to the same on the same command line. I use version 2.xxx at work and it doesnt work there.

On ksh, I seem to remember that it also depends on your version- what version are you using?

I suggest you go with Klashxx's perl pie solution, but adapted so that you use a separator char which isnt so usual in filenames:

perl -p -i -e "s|$replacethis|$withthis|g" filename

I agree the perl route is better ... it also works from within a shell as well. I was just trying to answer the original question, there may be other reasons as to why they need it to be part of a script. (like a class assignment)

I am running bash 3.2.5 & kde 93

This is a sed solution using backup file.

sed -i .bak "s/'$old'/'$new'/g" $1 && rm $i.bak

i try all the method but the one that work was the perl command

big thanks to all

:b::b::b::b: