Solved

Simple? Sed command

Posted on 2004-10-05
11
339 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
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
 
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

912 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

26 Experts available now in Live!

Get 1:1 Help Now