-   Programming (
-   -   Find and Replace problem at Shell Script (

TzaB 12-26-2008 11:39 AM

Find and Replace problem at Shell Script
Hello all! I am creating a shell script but i have a problem with 2 lines.
I want from the script, to search at a specific file and find one string that i put and replace it with something else.
For this action, i use sed command. Here is what i did and i will describe the problem better after the code.


echo "please tell me your Wan IP. You can find it at"
read wanip
cd /home/$user/$folder/gameserver/config
sed -i s/ExternalHostname=*/ExternalHostname=$wanip/
sed -i s/InternalHostname=*/InternalHostname=

At the file there are 2 lines with the strings:

So with the above code, i have the problem that is putting the $wanip at ExternalHostname and the at the InternalHostname, but is not replacing the symbol *

So after executing the script i have the following result at the file:

I don't want the asterisk. Is there someone that knows what i have to write in order to have the correct result (Without the asterisk at the end) ?

Thank you in advance.

pixellany 12-26-2008 11:52 AM

"*" in SED (and many other places) is a special character meaning "any number of instances" of the previous regular expression".

To use a literal "*", you need to escape it like so: "\*"


sed 's/old*/new/' ##replaces any occurence of "ol" plus any number of "d"s

sed 's/old\*/new/' ##replaces the string "old*"

Telemachos 12-26-2008 11:59 AM

As Pixellany said, the * is special and giving you problems. Additionally, your solution has another problem I think. If you run the script a second time, the lines won't look the same (they won't have the placeholder *, but an ip number). So the replacements may not do what you want in subsequent runs. I'm not a sed expert, but I tested this, and it should replace the entire line each time. That way you can use the script over and over.

## What you had before, then this
sed -i s/^External.*$/External=$wanip/
sed -i s/^Internal.*$/Internal=

TzaB 12-27-2008 06:16 AM


Thanks for your advice but the script that i am writing is an "installer" so there is no need to run it second time.


Thank you for the answer. I tried before create this thread only "*", without \

Thanks for the help and your time :)
Have a good day and happy Holidays!

All times are GMT -5. The time now is 09:07 AM.