LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2010, 10:00 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
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?

Last edited by trist007; 01-15-2010 at 10:09 PM.
 
Old 01-16-2010, 02:11 AM   #2
Web31337
Member
 
Registered: Sep 2009
Location: Russia
Distribution: Gentoo, LFS
Posts: 399
Blog Entries: 71

Rep: Reputation: 65
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.
 
Old 01-16-2010, 04:59 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 01-16-2010, 07:30 AM   #4
emi_ramo
Member
 
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213

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


Reply



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
help with sed khill Linux - General 4 08-15-2008 11:35 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:58 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
Open Source Consulting | Domain Registration