LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-17-2004, 05:42 PM   #1
Hcman
Member
 
Registered: Jan 2003
Location: Holland
Distribution: OpenBSD 3.6 - Arch Linux 0.7
Posts: 210

Rep: Reputation: 30
replacement with sed: replace pattern with multiple lines


I'd like to write a bash script that reads through an html template and inserts a random fortune (or any piece of text) into this template. I've been trying to do this with sed and I can get simple stuff like a date to work. Multiple lines don't seem to work however.

This works:
Code:
sed "s/DATE/`date`/" test.html > tmp.html
This doesn't work (for the most part):
Code:
sed "s/FORTUNE/`fortune -s`/" test.html > tmp.html
n.b. a oneliner works but not multiple line quotes.

After googling this it does seem sed is the tool for the job so I'm wondering what I'm doing wrong. Any advice would be greatly appreciated.


Arjan
 
Old 11-17-2004, 06:18 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Hmm..
I would use m4 to the same trick.
(or JSP/PHP oneliner, if available)

A sample usage of m4:
File foo.m4:
Code:
<html>
<body>
This is a document..<p>
A fortune:
<pre>esyscmd(fortune -s)</pre>
</body>
</html>
and to generate webpage:
Code:
m4 foo.m4 > foo.html
Try googling for "m4 write html" for some more documentation.
 
Old 11-17-2004, 06:30 PM   #3
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
... to do the thing with sed:
Code:
sed s/FORTUNE/"`fortune -s | sed 's/$/\\\\n/' | tr -d '\n'`"/ test.html > tmp.html
(see that there are three different types of quotes there).
 
Old 11-18-2004, 03:38 AM   #4
Hcman
Member
 
Registered: Jan 2003
Location: Holland
Distribution: OpenBSD 3.6 - Arch Linux 0.7
Posts: 210

Original Poster
Rep: Reputation: 30
Thanks! That works.
I'd been trying but never came close to that solution HA!

Arjan
 
Old 11-18-2004, 06:23 AM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
You could use perl too :
Code:
perl -pe 's/FORTUNE/`fortune`/e' test.html > tmp.html
 
Old 11-18-2004, 07:40 AM   #6
Hcman
Member
 
Registered: Jan 2003
Location: Holland
Distribution: OpenBSD 3.6 - Arch Linux 0.7
Posts: 210

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Cedrik
You could use perl too :
Code:
perl -pe 's/FORTUNE/`fortune`/e' test.html > tmp.html
No way, that is almost too easy :-)
Thanks for this suggestion, I hadn't though of perl.

Arjan
 
  


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
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
Replace every other occurrence of pattern Wynd Linux - General 8 12-14-2005 03:43 PM
search for pattern in files and replace mizuki26 Linux - Newbie 3 01-04-2004 11:57 AM
Replace lines in script AMMullan Linux - Software 9 12-06-2003 11:36 PM
replacing pattern with sed produces double realos Programming 1 10-17-2002 08:03 PM


All times are GMT -5. The time now is 10:31 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration