Solved

Simple? Sed command

Posted on 2004-10-05
11
338 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
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 10

Expert Comment

by:Luxana
Comment Utility
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
Comment Utility
\ 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
Comment Utility
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
 
LVL 11

Expert Comment

by:avizit
Comment Utility
#!/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
Comment Utility
Avizit:

You mean I will have to \ all the special charators that are in the CONTENTS of my file ?     Ouchy.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:TIMFOX123
Comment Utility
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
Comment Utility
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
Comment Utility
>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 200 total points
Comment Utility
> 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 100 total points
Comment Utility
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
Comment Utility
"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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now