Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed find/replace 2

Status
Not open for further replies.

ksbrace

Programmer
May 13, 2000
501
US
Hello,
I'm a newbie to shell scripting and was given the task to work on some scripts that are not working correctly.

The script is doing a find/replace, but it's doing too good of a job, if that is possible.

Code:
VARIABLES="ORA_HOST Z39_HOST [URL unfurl="true"]WWW_HOST[/URL] ORACLE_SID"

#Use sed to search for variable definition and replace with
#user defined variable

for VAR in $VARIABLES
do
  VALUE=""
  case $VAR in
     ORA_HOST)          VALUE=$ORA_HOST;;
     Z39_HOST)          VALUE=$Z39_HOST;;
     [URL unfurl="true"]WWW_HOST)[/URL]          VALUE=$[URL unfurl="true"]WWW_HOST;;[/URL]
     ORACLE_SID)        VALUE=$ORACLE_SID;;
  esac

$SED 's/^.*setenv.*[^{]'${VAR}'[^}].*/        setenv    '${VAR}'     '${VALUE}'/
' $ALEPH_START > $ALEPH_START_TEMP

$DIFF $ALEPH_START $ALEPH_START_TEMP >> $LOG
$MV $ALEPH_START_TEMP $ALEPH_START >> $LOG 2>&1
done
echo "-------------------------------------------------------" >> $LOG
I want it to change this line:
setenv ORACLE_SID aleph14

But not this one:
setenv ORACLE_ALERT_LOG $ORACLE_BASE/admin/$ORACLE_SID/bdump/alert_${O
RACLE_SID}.log

Any help would be greatly appreciated. Thanks in advance.
Kelly
 
Replace this:
[^{]
with this:
[^{\$]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hello, I have a similar situation. I have commented the line I want to change and the line I want to leave as is. As of right now, the code is doing too much, it's changing both lines and I just want it to change the first one.
Code:
##Substitute SIDE and CAMP with the appropriate values.
 source /aleph/SIDE/CAMP/alephe/aleph_start
        source $aleph_proc/def_local_env

        set old_alephver_alias = `alias alephver`
        alias alephver 'echo "xxxx" > /dev/null'

##Leave the line below AS IS!
setenv suny_aleph_app_version ${ALEPH_CAMP_SIDE}

Current code:
Code:
$SED 's/'${START_SIDE}'/'${DEST_SIDE}'/' $ALEPH_SHUTDOWN > $ALEPH_SHUTDOWN_TEMP

My attempt at fixing it:
Code:
$SED 's[^ALEPH]/'${START_SIDE}'/'${DEST_SIDE}'/' $ALEPH_SHUTDOWN > $ALEPH_SHUTDOWN_TEMP

Any help would be greatly appreciated. Thanks,

 
Hi

Wrong syntax of the [tt]s///[/tt] command. You put the character enumeration between the command name ( [tt]s[/tt] ) and the first delimiter ( [tt]/[/tt] ). Move them after it.

Feherke.
 
Ok, I made the change correctly, I think...but it's still replacing the line I don't want it to replace. any help would be great!

Code:
$SED 's/[^ALEPH]'${START_SIDE}'/'${DEST_SIDE}'/' $ALEPH_SHUTDOWN > $ALEPH_SHUTDOWN_TEMP

$DIFF $ALEPH_SHUTDOWN $ALEPH_SHUTDOWN_TEMP >> $LOG
$MV $ALEPH_SHUTDOWN_TEMP $ALEPH_SHUTDOWN >> $LOG 2>&1


Code:
##Substitute SIDE and CAMP with the appropriate values.
 source /aleph/SIDE/CAMP/alephe/aleph_start
        source $aleph_proc/def_local_env

        set old_alephver_alias = `alias alephver`
        alias alephver 'echo "xxxx" > /dev/null'

##Leave the line below AS IS!
setenv suny_aleph_app_version ${ALEPH_CAMP_SIDE}

 
Hi

This ALEPH is a literar string which should not be in front of the replaceable exression ? Just because [tt][^ALEPH][/tt] means none of the enumerated characters, not as you ( I think ) wants, not the given literar string.

As far as I know, this is not possible with [tt]sed[/tt]. I propose to try [tt]perl[/tt] :
man perlre said:
"(?<!pattern)"
A zero-width negative look-behind assertion. For example
"/(?<!bar)foo/" matches any occurrence of "foo" that does not
follow "bar". Works only for fixed-width look-behind.
Code:
perl -pe 's/(?<!ALEPH)'${START_SIDE}'/'${DEST_SIDE}'/' $ALEPH_SHUTDOWN > $ALEPH_SHUTDOWN_TEMP

Feherke.
 
Feherke,
I would love to be able to use perl, but the boss wants to maintain the shell scripts. So I need to do his way. His quote "There are 3 ways of doing things: the right way, the wrong way, and his way" So, if anyone else has some helpful tips on how to fix this, it would be greatly appreciated. Thanks in advance!


 
Hi

And what is the difference between using 1 [tt]sed[/tt] command and using 1 [tt]perl[/tt] command for the string substitution ? Ok, let it lost.

If you are sure, the [tt]${START_SIDE}[/tt] string appears only once per line, you could check that in the address part :
Code:
$SED '/ALEPH'${START_SIDE}'/!s/'${START_SIDE}'/'${DEST_SIDE}'/' $ALEPH_SHUTDOWN > $ALEPH_SHUTDOWN_TEMP

Feherke.
 
feherke,
Thanks for your help! I tried the sed command from the previous post:
Code:
$SED '/ALEPH'${START_SIDE}'/!s/'${START_SIDE}'/'${DEST_SIDE}'/' $ALEPH_SHUTDOWN > $ALEPH_SHUTDOWN_TEMP
But that didn't work.
it's changing the line to:
setenv suny_aleph_app_version ${ALEPH_itx_dev}
I need this line to stay as is:
setenv suny_aleph_app_version ${ALEPH_CAMP_SIDE}

So, I decided to be a 'rebel' and inserted the perl command:
Code:
perl -pe 's/(?<!ALEPH)'${START_SIDE}'/'${DEST_SIDE}'/' $ALEPH_SHUTDOWN > $ALEPH_SHUTDOWN_TEMP
it also changed the line I didn't want to change.

Any other ideas?

Code:
##Substitute SIDE and CAMP with the appropriate values.
 source /aleph/SIDE/CAMP/alephe/aleph_start
        source $aleph_proc/def_local_env

        set old_alephver_alias = `alias alephver`
        alias alephver 'echo "xxxx" > /dev/null'

##Leave the line below AS IS!
setenv suny_aleph_app_version ${ALEPH_CAMP_SIDE}

 
Hi

Honestly, I did not understand the situation, I just modified your [tt]sed[/tt] command.

- What are the [tt]$START_SIDE[/tt] and [tt]$DEST_SIDE[/tt] variable values ?
- If would be to change that file by hand, not with a script, how you know which line to change ?

Hmm... I think I completely missed the point.

Feherke.
 
$START_SIDE=SIDE
$DEST_SIDE=dev or prod

I guess I could add a replacment line at the end that goes back and changes ALEPH_itx_dev to its original state, but that does'nt seem to be the correct way of doing it. Thanks.

 
Why not simply this ?
$SED "/ALEPH_.*$START_SIDE/!s/$START_SIDE/$DEST_SIDE/" $ALEPH_SHUTDOWN > $ALEPH_SHUTDOWN_TEMP

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

So the "ALEPH" string is not exactly before the replaceable part, there is at least an underscore, or even the string "CAMP" between them ? Then say it that there could be, lets say, up to 6 other characters between the two strings :
Code:
$SED '/ALEPH[red].\{0,6\}[/red]'${START_SIDE}'/!s/'${START_SIDE}'/'${DEST_SIDE}'/' $ALEPH_SHUTDOWN > $ALEPH_SHUTDOWN_TEMP

Feherke.
 
Feherke & PHV,
Many thanks, both ways seem to work just fine. I'm learning more and more each day with these scripts, so hopefully my job will eventually become a bit more relaxing. These scripts are for opening libraries with exlibris software...if youcare. Thanks again!



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top