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
Solved

Simple? Sed command

Posted on 2004-10-05
11
343 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

808 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