Results 1 to 2 of 2
I am creating a sort of database. 1 thing i want to allow the user to do is enter a word within the database they want to replace, then they ...
- 04-05-2008 #1Just Joined!
- Join Date
- Apr 2008
- Posts
- 4
user input swapping
I am creating a sort of database. 1 thing i want to allow the user to do is enter a word within the database they want to replace, then they have to enter the word they want to replace it with, and once this is done, the swap should take place. The code i am trying to do this with is:
echo please enter the word to be replaced
read str
grep -i $str database
echo new word
read no
sed 's/str/no/g' database > database2
mv database2 database
This doesnt seem to work though, can any1 help me out please.
- 04-05-2008 #2
This certainly does not work. You are relying on sed to do your replacement, but look at the line you have given it:
This means "look for every occurrence of 'str' and replace it with 'no'"Code:sed 's/str/no/g' database
What you actually want is:
Two changes:Code:sed "s/$str/$no/g"
1) Use the variables $str and $no rather than the literal strings "str" and "no".
2) Single Quotes (') to Double Quotes ("). Single quotes do not allow for variable interpolation.DISTRO=Arch
Registered Linux User #388732


Reply With Quote