LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed -i switch and here document not working :(:(:( (https://www.linuxquestions.org/questions/linux-newbie-8/sed-i-switch-and-here-document-not-working-902898/)

sysmicuser 09-13-2011 10:37 PM

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.

grail 09-14-2011 02:32 AM

How is a here document equivalent to a file??

sysmicuser 09-14-2011 04:58 AM

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


Your help would me much appreciated.

crts 09-14-2011 07:55 AM

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.

sysmicuser 09-14-2011 09:31 AM

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?

crts 09-14-2011 09:42 AM

Quote:

Originally Posted by sysmicuser (Post 4471489)
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.

sysmicuser 09-14-2011 10:00 PM

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.

jschiwal 09-14-2011 10:16 PM

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>'


grail 09-14-2011 11:21 PM

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


sysmicuser 09-15-2011 07:46 AM

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:):)

crts 09-15-2011 07:56 AM

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.

grail 09-15-2011 08:37 AM

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.

sysmicuser 09-16-2011 07:49 AM

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............................

sysmicuser 09-16-2011 08:46 AM

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>

grail 09-16-2011 09:42 AM

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??


All times are GMT -5. The time now is 05:27 PM.