LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I need using sed... (https://www.linuxquestions.org/questions/programming-9/i-need-using-sed-782516/)

trist007 01-15-2010 10:00 PM

I need using sed...
 
I need to basically add some code to like 50 php files. It's basically a script that I want to add near the end of these 50 php files. I figured I could use sed to replace </body> with the code. However the code, is very long and has a bunch of newlines obviously. How would I be able to do this?

sed -e 's/<//body>/(all the text)/' *.php

Since it has newlines, I don't know how to paste it in cause it would take me to a '>' prompt without allowing me to complete the sed command.

Gonna try manually putting in the newlines.

I want to insert this code.

Code:

<br><br>
<center>
<form action="test.php" method=post>
<textarea name="comments" cols=40 rows=6></textarea>
<p>
<input type="submit" value="post comment">
</form>
</center>
<br><br>
<?php
$file = fopen("test","r");

while(! feof($file))
  {
  echo fgets($file). "<br />";
  }

fclose($file);
?>
</body>
</html>

Seems like the chars '<>/' would be escape characters. Do I just add a '\' in front of each one?

Web31337 01-16-2010 02:11 AM

basically you can use other separator than / for a replace expression, say ^ if it's never met in your replace code and linebreaks could be replaced with \n.

pixellany 01-16-2010 04:59 AM

Why not use the "r" command to read in the data from a file?

See this tutorial: http://www.grymoire.com/Unix/Sed.html ---> Reading in a file with the 'r' command

emi_ramo 01-16-2010 07:30 AM

What about something like this? Edit a file, put these lines and execute it:
Code:

#!/bin/bash

ls *.php | while read file; do
cat $file | sed -e 's/<\/body>/<br><br> \
<center>\
<form action="test.php" method=post>\
<textarea name="comments" cols=40 rows=6><\/textarea>\
<p>\
<input type="submit" value="post comment">\
<\/form>\
<\/center>\
<br><br>\
<?php\
$file = fopen("test","r");\
\
while(! feof($file))\
  {\
  echo fgets($file). "<br \/>";\
  }\
\
fclose($file);\
?>\
<\/body>\
/' > $file.new
mv $file.new $file
done



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