LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-17-2011, 05:13 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Rep: Reputation: 0
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.
 
Old 06-17-2011, 06:35 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,515

Rep: Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892
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.
 
Old 06-17-2011, 07:00 AM   #3
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
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.
 
Old 06-17-2011, 07:09 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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

Last edited by Snark1994; 06-17-2011 at 07:10 AM.
 
Old 06-17-2011, 07:21 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,515

Rep: Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892
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
 
1 members found this post helpful.
Old 06-17-2011, 07:39 AM   #6
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
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.
 
Old 06-17-2011, 07:45 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,515

Rep: Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892
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
 
1 members found this post helpful.
Old 06-17-2011, 08:14 AM   #8
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
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?

Last edited by sysmicuser; 06-17-2011 at 08:18 AM.
 
Old 06-17-2011, 08:36 AM   #9
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
Got that but new challenge but that would be in new thread!

Thank you for your assistance.
 
Old 06-17-2011, 08:40 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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.
 
1 members found this post helpful.
Old 06-17-2011, 09:52 AM   #11
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
Thanks jschiwal I commited that mistake before your post small little helps in a big learning curve!
 
Old 06-18-2011, 10:39 AM   #12
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by sysmicuser View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
sed saves w/o a change Clutch2 Linux - General 7 12-26-2009 08:45 AM
Ask confirmation for every change performed by Sed script raimizou Programming 3 04-03-2009 05:03 AM
Sed - change some thing but not all mijohnst Linux - Newbie 5 09-01-2007 04:01 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:12 PM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration