Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple? Sed command

Posted on 2004-10-05
11
Medium Priority
?
348 Views
Last Modified: 2010-04-20
Lets see if I can totally screw up this explination again.

I have one file, lets call it "template.html".
I want to put sentance "replace this line" in template.html
I have another file, lets call it " text_of_HTML_page"

Now with a Sed command I want to put the file "template.html" and the contents of " text_of_HTML_page" where the phrase  "replace this line"  was and output it to index.html

If that was clear as mud, this is my try:

sed s/ "replace this line"/ $(cat text_of_HTML_page )/g > index.html

I know I need many \  marks in this but left them out so it could be understood.  


To explain this in a differnet way, I want to find a nice template for a web page that uses a fancy editor I do not have.   I want to use a simple editor to create the content for each page.   I then want to insert the content into someone else's nicely create page.  

















0
Comment
Question by:TIMFOX123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 10

Expert Comment

by:Luxana
ID: 12233674
Hi  TIMFOX123,

sed will never edit your current file instead of trying to edit current file you can use STDIN | STDOUT for example to change string wich match in line you can use:

cat index.html | sed '/matchstring/replacewithwhat/' > index_new.html
mv index_new.html to index.html

g option is used to replace avery match in line withoot g you replace just first match in line

Luxana
0
 
LVL 10

Expert Comment

by:Luxana
ID: 12233707
\ character is escape character for example you  want replace "/" in line so can't do:

sed 's///something/'
instead of that you need to use \ escape character:

sed 's/\//something/'

so / will be not read us metacharacter but will read literaly.

0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12233712
There are several more appropriate ways to do this:

- Use awk instead of sed.  However "replace this line" should be on a line by itself.

- Slit the template in two: template_pre.html and template_post.html, then use 'cat'
to join together the three parts.


But these would be most preferable (from a cleanliness point of view):

- Use PHP to include your text into the template html file.

- Use CSS to specify all the 'nicely created layout', simplifying your html so you can
use a simple editor.

0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 11

Expert Comment

by:avizit
ID: 12233734
#!/bin/bash
rep=`cat text_of_HTML_page`
echo $rep
sed -e "s/replace this line/${rep}/g" template.html > index.html


try above

warning: you may have to escape all those characters in text_of_HTML_page,
so basically your approach is not advisable
0
 

Author Comment

by:TIMFOX123
ID: 12234168
Avizit:

You mean I will have to \ all the special charators that are in the CONTENTS of my file ?     Ouchy.
0
 

Author Comment

by:TIMFOX123
ID: 12234183
Brett:

I thought about the PHP but is a whole lot simpler.  Also I can put the contents up on a free site that does not have PHP.  

How would I do it with awk ?
0
 
LVL 11

Expert Comment

by:avizit
ID: 12234231
yes seems like you have to escape all the speial characters in "text_of_HTML_page"

and end all lines in the file with a "\"

so yeah.. not a neat solution and obviously not the way to do it :)
but still the script seems to work :)

0
 
LVL 10

Expert Comment

by:Luxana
ID: 12234396
>You mean I will have to \ all the special charators that are in the CONTENTS of my file ?     Ouchy.

for this case you can also use sed for example to excape "." you can use:

cat text_of_HTML_page | sed 's/\./\\./g' > text_of_HTML_page_escaped
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 800 total points
ID: 12234462
> How would I do it with awk ?

awk '$0 == "replace this line"  { system("cat text_of_HTML_page"); next; }'  template.html > index.html

This is preferable to the command substitutions in sed, which will fail if the substitution contains a slash.



0
 
LVL 5

Assisted Solution

by:pbhj
pbhj earned 400 total points
ID: 12236129
I'd go with the php option as well ... how difficult is <?php include('filename.txt'); ?> after all.

Lycos do a free PHP and MySQL account along with about a million others.

Respect for trying to learn sed and awk, I'm not the hacker that you are! I have used the KFileReplace utility in KDE recently for editting HTML and found it quite easy if a little heavy on processing.

HTH

pbhj
0
 

Author Comment

by:TIMFOX123
ID: 12236298
"This is preferable to the command substitutions in sed, which will fail if the substitution contains a slash."
 

This would have taken me a week to figure out !!!

Thank you very very much.

Dave
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question