LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed script to change the value (https://www.linuxquestions.org/questions/linux-newbie-8/sed-script-to-change-the-value-886846/)

sysmicuser 06-17-2011 05:13 AM

sed script to change the value
 
Hi,

I need your help as I am weak in sed and regular expressions.

I am trying to alter the value of one configuration file from XML to LDAP.(Oracle Application Server)
sed -e 's/XML/LDAP/g' $ORACLE_HOME/j2ee/${OC4J_INSTANCE}/application-deployments/SPLWebApp/orion-application.xml

Oracle Home and OC4J Instance Values would be passed to script.

The actual node in file(orion-application.xml) look's like this,
<jazn provider="XML" /> so this is to be change to <jazn provider="LDAP" />

I have question to you what I have done is there any better way? any more defensive technique I can use with this?

Thank you for your assistance.

grail 06-17-2011 06:35 AM

Well as there is only one on the line the 'g' at the end is not required. The only thing to make it stricter would be to include the entire entry.

sysmicuser 06-17-2011 07:00 AM

Rightly said that way we can make it more defensive however it gives me error as now i do like this.

sed -e 's/<jazn provider="XML" />/<jazn provider="LDAP" />//' $ORACLE_HOME/j2ee/${OC4J_INSTANCE}/application-deployments/SPLWebApp/o
rion-application.xml
sed: -e expression #1, char 27: unknown option to `s'

Can you please tell me in sed when you put \? I am confused when you put \.
To my little knowledge you put \ just before space " " -- quotes removed, =,!,#,$,%,^,&,*,(,) that is for all special characters right?

Please guide.

Thanks.

Snark1994 06-17-2011 07:09 AM

It's complaining about the '/'. Change it to:
Code:

sed -e 's/<jazn provider="XML" \/>/<jazn provider="LDAP" \/>//' $ORACLE_HOME/j2ee/${OC4J_INSTANCE}/application-deployments/SPLWebApp/o
rion-application.xml

Oops, rereading your post you mentioned you knew this. Yes, you do need to escape forward slashes as well, as you use the '/' to separate parts of the command

grail 06-17-2011 07:21 AM

Or the nice thing about sed when do a substitution you can simply change the delimiter:
Code:

sed -e 's@<jazn provider="XML" />@<jazn provider="LDAP" />@' $ORACLE_HOME/j2ee/${OC4J_INSTANCE}/application-deployments/SPLWebApp/orion-application.xml

sysmicuser 06-17-2011 07:39 AM

Thank you Snark and grail

Snark If i Run the way it was given by yourself.
sed -e 's/<jazn provider="XML" \/>/<jazn provider="LDAP" \/>//' $ORACLE_HOME/j2ee/${OC4J_INSTANCE}/application-deployments/SPLWebApp/orion-application.xml

Then changed it to,
sed -e 's/<jazn provider="XML" \/>/<jazn provider="LDAP" \/>/' $ORACLE_HOME/j2ee/${OC4J_INSTANCE}/application-deployments/SPLWebApp/orion-application.xml

why it worked later and not the former? I feel the former shoudl also work? instead of g we are having nothing "/" so why shell complains?

grail the one way gave so ran without any issues.

Thanks Guys.

grail 06-17-2011 07:45 AM

Quote:

why it worked later and not the former?
The former has an extra / at the end:
Code:

sed -e 's/<jazn provider="XML" \/>/<jazn provider="LDAP" \/>//' $ORACLE_HOME/j2ee/${OC4J_INSTANCE}/application-deployments/SPLWebApp/orion-application.xml

sysmicuser 06-17-2011 08:14 AM

Thanks grail! :)

Your logic also helped me in another automation! :):):)
Have a look at this
<session-timeout>any damn value donesn't mattter</session-timeout> to be changes to
<session-timeout>-1s</session-timeout>

I did like this,

sed -e 's@<session-timeout>*.*</session-timeout>@<session-timeout>-1s</session-timeout>@' file.xml

ant it works well.

My question,
1) Is there any other way to do
2) Most important once script is run is the file going to be saved with new values? without altering the other contents of file ? how can we achieve that?

sysmicuser 06-17-2011 08:36 AM

Got that but new challenge but that would be in new thread!

Thank you for your assistance.

jschiwal 06-17-2011 08:40 AM

For GNU See you can use the -I option to edit the file in place. You can also redirect the output to a file. Don't redirect the output. To the same file as the input or you will replace the input file with an empty file before see runs.

sysmicuser 06-17-2011 09:52 AM

Thanks jschiwal I commited that mistake :( before your post :( small little helps in a big learning curve! :)

Snark1994 06-18-2011 10:39 AM

Quote:

Originally Posted by sysmicuser (Post 4388507)
why it worked later and not the former? I feel the former shoudl also work? instead of g we are having nothing "/" so why shell complains?

Very sorry... I need to start checking my posts more carefully, second time I've done something like that :) just as grail said, I put in an extra slash.


All times are GMT -5. The time now is 09:35 PM.