Solved

Simple? Sed command

Posted on 2004-10-05
11
345 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

751 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