LinuxQuestions.org
Have you listened to LQ Radio?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Tags used in this thread
Popular LQ Tags , , ,

Reply
 
Thread Tools
Old 12-26-2008, 12:39 PM   #1
TzaB
LQ Newbie
 
Registered: Jul 2008
Location: Greece
Distribution: Debian 5.0
Posts: 3
Thanked: 0
Find and Replace problem at Shell Script


[Log in to get rid of this advertisement]
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.

Code:
echo "please tell me your Wan IP. You can find it at www.WhatIsMyIP.com"
read wanip
cd /home/$user/$folder/gameserver/config
sed -i s/ExternalHostname=*/ExternalHostname=$wanip/ server.properties
sed -i s/InternalHostname=*/InternalHostname=127.0.0.1/ server.properties
At the file server.properties there are 2 lines with the strings:
ExternalHostname=*
InternalHostname=*

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

So after executing the script i have the following result at the file:
ExternalHostname=WHAT_$WANIP_HAS*
InternalHostname=127.0.0.1*

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.
TzaB is offline  
Tag This Post , , ,
Reply With Quote
Old 12-26-2008, 12:52 PM   #2
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch
Posts: 13,165
Thanked: 285
"*" 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: "\*"

Examples:

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

sed 's/old\*/new/' ##replaces the string "old*"
pixellany is offline     Reply With Quote
Old 12-26-2008, 12:59 PM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 686
Thanked: 47
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.
Code:
## What you had before, then this
sed -i s/^External.*$/External=$wanip/ server.properties
sed -i s/^Internal.*$/Internal=127.0.0.1/ server.properties
Telemachos is offline     Reply With Quote
Old 12-27-2008, 07:16 AM   #4
TzaB
LQ Newbie
 
Registered: Jul 2008
Location: Greece
Distribution: Debian 5.0
Posts: 3
Thanked: 0

Original Poster
@Telemachos

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

@pixellany

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!
TzaB is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to replace value of 4.1.0.1.22 to 4.1.0.0.22 using shell script amit_pansuria Programming 3 08-06-2008 09:30 PM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 11:37 PM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 04:07 AM
Shell script to find/replace build new TAB record ljungers Programming 6 01-19-2007 05:47 PM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 02:01 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration