LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-13-2011, 11:37 PM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 334

Rep: Reputation: 0
Angry sed -i switch and here document not working :(:(:(


Here is what I am trying to do.
Code:
sed -i "/<web-app>/r TEXT_TO_BE_CAPTURED" ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/SPLApp/orion-web.xm
l << TEXT_TO_BE_CAPTURED

                <session-config>
                        <session-timeout>-1s</session-timeout>
                </session-config>

TEXT_TO_BE_CAPTURED
and it is not working

Basically I want to grab that text in here's document and feed in to sed with i and /r somehow it is not working.....


Your assistance is highly appreicated.

Thank you.
 
Old 09-14-2011, 03:32 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
How is a here document equivalent to a file??
 
Old 09-14-2011, 05:58 AM   #3
sysmicuser
Member
 
Registered: Mar 2010
Posts: 334

Original Poster
Rep: Reputation: 0
Unhappy

May be I am an idiot, please educate me how to do with here document.


Your help would me much appreciated.
 
Old 09-14-2011, 08:55 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi,

the -i option only edits files. A here document is not a file, hence the -i option is useless.
What you probably want is something like this:
Code:
$ sed 's/hello/hi/' << EOF > outputfilename
hello
there
EOF

$ cat outputfilename 
hi
there
Notice the bold part. It marks the end of input and returns the bash prompt.
 
Old 09-14-2011, 10:31 AM   #5
sysmicuser
Member
 
Registered: Mar 2010
Posts: 334

Original Poster
Rep: Reputation: 0
I don't think s switch would work here.
I want to edit the file/update the file and save.
The file would be having node as follows:
<webapp>
</webapp>
My aim is to edit/update file such that it would end up with following.
<webapp>
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
</webapp>

This is what I want to achieve on the side not when should we use here document?
 
Old 09-14-2011, 10:42 AM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Quote:
Originally Posted by sysmicuser View Post
I don't think s switch would work here.
I want to edit the file/update the file and save.
The file would be having node as follows:
<webapp>
</webapp>
My aim is to edit/update file such that it would end up with following.
<webapp>
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
</webapp>

This is what I want to achieve on the side not when should we use here document?
I was merely providing an example on how to use here documents with sed. Use whatever commands you deem fit to achieve your goal. From your examples, I do not understand what you are actually trying to do.
 
Old 09-14-2011, 11:00 PM   #7
sysmicuser
Member
 
Registered: Mar 2010
Posts: 334

Original Poster
Rep: Reputation: 0
I was merely providing an example on how to use here documents with sed. Use whatever commands you deem fit to achieve your goal.
Quote:
Thank you very much, it is much appreciated.
From your examples, I do not understand what you are actually trying to do.
Quote:
I am basically trying to automate post, ear deployment task
When a ear file is deployed onto Application server, the cofiguration file orion-web.xml has a node called
<webapp>
</webapp>
I want the text in discussion to be inserted in between <webapp> and </webapp>

I wish to implement some intelligent scripting mechanism without using any other external file(without cat).

Hope I have made it clear.

Thanks.
 
Old 09-14-2011, 11:16 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Instead of a HERE document, insert the sed command needed into your script:
Code:
sed -i '/<web-app>/a\
                <session-config>\
                        <session-timeout>-1s</session-timeout>\
                </session-config>\
\' ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/SPLApp/orion-web.xml

Add the lines for the text you want to add, and escape the return character with a backslash.
It will be as easy to edit your script as a here document inside a script would be.

Another option, since you know the contents of the file, is to replace the file in it's entirety.
Code:
echo > ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/SPLApp/orion-web.xml '<webapp>'
echo >>  ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/SPLApp/orion-web.xml << EOF
                <session-config>
                        <session-timeout>-1s</session-timeout>
                </session-config>
EOF
echo >> ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/SPLApp/orion-web.xml '</webapp>'

Last edited by jschiwal; 09-14-2011 at 11:34 PM.
 
Old 09-15-2011, 12:21 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
A third choice would be assuming you wish to use the /r option of sed is to actually place the text in a file and call
it as you have done:
Code:
$ cat session.config
                <session-config>
                        <session-timeout>-1s</session-timeout>
                </session-config>
$ sed -i '/<webapp>/r session.config/' ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/SPLApp/orion-web.xml
 
Old 09-15-2011, 08:46 AM   #10
sysmicuser
Member
 
Registered: Mar 2010
Posts: 334

Original Poster
Rep: Reputation: 0
It seems I'm leading nowhere cooking slowly n steady.

@jschiwal
Your first method,work's fine but in document/configuration file I can see text but with appended "\" moreover If I run script multiple times, multiple instances of configuration text is observed
I tried your second implementation, exactly as you prescribed, however the resultant file has only
Quote:
<webapp>

</webapp>
and nothing else.

@grail

I did something like this.
Quote:
cat session.config << EOF
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
EOF
sed -i '/<webapp>/r session.config/' ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/SPLApp/orion-web.xml
After runing script it gives me error message.
Quote:
cat: session.config: No such file or directory
Now, I am bit upset because the one which I am giving below was working but today it is not working!!!
Quote:
sed -i 's@<jazn provider=".*"/>@<jazn provider="LDAP"/>@' ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/orion-application.xml

echo $?
File has a node which looks like this.
Quote:
<jazn provider="XML" />

It returns zero as retun code but the configuration file is unchanged!! If I cannot implement simple subitute with sed, it is disgusting.

Please help me learn the evil sed, but no wonder it is super powerful!!

Thanks guys your assistance is much appreciated
 
Old 09-15-2011, 08:56 AM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
So, if I understand correctly then you are not trying to edit a here document but the here document is supposed to be read by sed's r command, right? Not sure why it has to be a here document but try this:
Code:
cat > session.config << EOF
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
EOF
sed -i '/<webapp>/r session.config' ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/SPLApp/orion-web.xml
The r command reads a file. It cannot read a here document because a here document is not a file. You can, however, redirect the contents of a here document to a file like shown above.

Last edited by crts; 09-15-2011 at 09:39 AM. Reason: fixed typo
 
Old 09-15-2011, 09:37 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
The issue with the sed and /r is there should be no slash at the end ... my bad:
Code:
sed -i '/<webapp>/r session.config' ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/SPLApp/orion-web.xml
Tested and works.
 
Old 09-16-2011, 08:49 AM   #13
sysmicuser
Member
 
Registered: Mar 2010
Posts: 334

Original Poster
Rep: Reputation: 0
Sorry Guys after all attempts it failing again again and again.

I can see the timestamp is updated but contents of file are not changed.

Piece of code exactly in script.

Quote:
cat > session.config << EOF
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
EOF
sed -i '/<webapp>/r session.config' ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/SPLApp/orion-web.xml
One more thing why this regex is not working!

Quote:
sed -i 's@<jazn provider=".*" />@<jazn provider="LDAP"/>@' ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/orion-appl
ication.xml
here we say "after = sign whatever character's do not worry,change value of XML to LDAP, why can't work???

Actual file,
Quote:
<jazn provider="XML" />
I know this can't be that hard, this is driving me crazy why the hell is this not working............................
 
Old 09-16-2011, 09:46 AM   #14
sysmicuser
Member
 
Registered: Mar 2010
Posts: 334

Original Poster
Rep: Reputation: 0
Quote:
cat > session.config << EOF
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
EOF
sed -i '/<web-app>/r session.config' ${ORACLE_HOME}/j2ee/OC4J_CCA/application-deployments/SPLWebApp/SPLApp/orion-web.xml
This one is working however for multiple run's it is introducing more that 1 instance of configuartion value, I want only one and one between <web-app> and </web-app>

Have a look at my post run file when ran twice!!

Quote:
<web-app>
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
</web-app>
WHat we need to achieve is like this

Quote:
<web-app>
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
</web-app>
 
Old 09-16-2011, 10:42 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
So there are a few confusing things here:

1. Start of post #13 says the code does not work and yet post #14 says it does??

2. If you run the code twice why would it not perform the task twice??
 
  


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
Sheet fed (or, document) scanners working with Linux ?? RonCam Linux - Hardware 3 07-20-2009 06:52 PM
sed with html document jpgauvin Linux - Newbie 4 12-22-2008 07:54 PM
html; character encoding per document tag, not whole document TheLinuxDuck Programming 0 08-14-2008 12:12 PM
SED - minor changes work - Larger doesn't (working and non working code included) Nimoy Programming 17 09-22-2007 05:34 PM
sed not working... culin Programming 26 02-07-2007 05:58 PM


All times are GMT -5. The time now is 11:21 AM.

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