Languages and Scripting

How to replace a string like $mystring in a file

 
SOLVED
Go to solution
SwissKnife
Frequent Advisor

How to replace a string like $mystring in a file

Hi,

 

Here is the content of a file:

...

CURRENT===BLKSIZE=1048576,SBT_LIBRARY=$ORACLE_HOME/lib/libddobk.so,ENV=(STORAGE_UNIT=oramtsrepl,BACKUP_HOST=dedcti1data,ORACLE_HOME=$ORACLE_HOME)

...

 

How to replace the string $ORACLE_HOME by the value of my environment variable in my file?

note: My environment variable is something like that: /logiciel/app/oracle/product/11.2.0.4

 

Kind regards,

Den.

 

 

 

4 REPLIES 4
Steven Schweda
Honored Contributor

Re: How to replace a string like $mystring in a file

> How to replace the string $ORACLE_HOME by the value of my environment
> variable in my file?

   sed?

> note: My environment variable is something like that:
> /logiciel/app/oracle/product/11.2.0.4

   What, exactly, is the problem?  Evaluating the environment variable,
or dealing with "/", or what?

mba$ export MEV='F/R/E/D'
mba$ echo $MEV
mba$ echo 'xxxx$O_Hyyyy' | sed -e 's|$O_H|'"$MEV"'|'
xxxxF/R/E/Dyyyy

SwissKnife
Frequent Advisor

Re: How to replace a string like $mystring in a file

Hi Steven,

 

Thank you.

 

I can simplify the problem with short code:

 

ORACLE_HOME='/logiciel/app/oracle/product/11.2.0.4'
PRM_PARMS='AA=$ORACLE_HOME is the value of ORACLE_HOME'
echo $PRM_PARMS | sed '.....What is the syntax here....'

echo should return:

AA=/logiciel/app/oracle/product/11.2.0.4 is the value of ORACLE_HOME

 

 

 

I have tried this (and a lot of other variations ;-) without success:

echo $PRM_PARMS | sed 's,'$ORACLE_HOME',$ORACLE_HOME,'

=> does not work

echo $PRM_PARMS | sed -e "s/\$ORACLE_HOME/$ORACLE_HOME/g"

=> does not work

 

 

Kind regards,

Den.

 

 

 

 

SwissKnife
Frequent Advisor
Solution

Re: How to replace a string like $mystring in a file

FYI,

 

Solved!!

echo $PRM_PARMS | sed -e "s+\$ORACLE_HOME+$ORACLE_HOME+g"

 

Kind regards,

Den.

Steven Schweda
Honored Contributor

Re: How to replace a string like $mystring in a file

> echo $PRM_PARMS | sed 's,'$ORACLE_HOME',$ORACLE_HOME,'

   You need to do something to keep the shell from treating the first
"$" (only) as special.  I used apostrophes.

> echo $PRM_PARMS | sed -e "s/\$ORACLE_HOME/$ORACLE_HOME/g"

   You can't use "/" for the sed:s delimiter if "/" appears in the
string.

> echo $PRM_PARMS | sed -e "s+\$ORACLE_HOME+$ORACLE_HOME+g"

   Yes, a "\" can do the first job instead of apostrophes, and "+" will
do the second job as well as "|", so long as neither appears in the
string.  And "g" will help if you have more than one instance.  You
could add a "g" to my suggestion, too.