If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Substring replacement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-26-11, 14:14
mail4mz mail4mz is offline
Registered User
 
Join Date: Aug 2010
Posts: 17
Substring replacement

Hi All,

I need replace a substring in shell script. The value of newString will be passed in as input argument. However the following script won't take the value of newString. Any suggestion?

Thanks,

Mike


newString=$1
oldSentence="This is an old way!"

echo $oldSentence | sed -e 's/old/$(newString)/g'
Reply With Quote
  #2 (permalink)  
Old 07-26-11, 14:19
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool

Try:

Code:
echo $oldSentence | sed -e "s/old/$(newString)/g"
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 07-26-11, 14:42
mail4mz mail4mz is offline
Registered User
 
Join Date: Aug 2010
Posts: 17
It works with double quotes! Thanks!

How do I replace "\" with "/" using sed?

Eg. oldstring="\asd\aaa\a.txt"

I want modify it to "/asd/aaa/a.txt"

echo oldstring | sed -e "s/\\/\//g" > newstring


Thanks!
Reply With Quote
  #4 (permalink)  
Old 07-26-11, 19:35
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
You can use any character in the substitute expression so:

Code:
sed -e ?\\abc?/abc?
Not using "/" as the delimiter, makes replacing "/" in the text much easier.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On