LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help - how do i run a multi-line sed command in perl? (https://www.linuxquestions.org/questions/programming-9/help-how-do-i-run-a-multi-line-sed-command-in-perl-760860/)

rma88 10-09-2009 04:40 PM

help - how do i run a multi-line sed command in perl?
 
hey everyone,

I'm writing a little perl script that is editing all my .php files and inserting a google-analytics piece of code before the </body> tag.

the sed command works just find from command line, and is such :

Code:

sed '/<\/body>/ i\
<script type="text/javascript"> \
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); \
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); \
</script> \
<script type="text/javascript"> \
try { \
var pageTracker = _gat._getTracker("UA-10500631-2"); \
pageTracker._trackPageview \
} catch(err) {}</script>
' MyPage.php

this works fine, however, how am I supposed to execute this in a perl script? I can't put it in a double quoted string, or a single quoted string, because the command contains both double and single quotes. Also, it spans multiple lines...

I have tried putting it in backticks over multiple lines just as is, that doesn't work. i've take out all the spaces and the '\' and made it a single, really long one line command and it doesn't work.

I'm at a loss, if anyone can show me what to do it would be really appreciated... this is the last little bit of the script, so any help is greatly appreciated!

ghostdog74 10-09-2009 06:53 PM

you don't have to execute that piece of sed code in Perl. You can do what that sed does with Perl as well.. either s/// or find the index where </body> lies and add text after it.

rma88 10-09-2009 07:11 PM

ah, very cool.

i'll go look at the s///.

ya i was going to avoid parsing the script in perl and adding the text because i already had a quick little sed command.

thanks, i'll check out s///.
much appreciated.


All times are GMT -5. The time now is 08:59 AM.