-   Programming (
-   -   Replacing a place holder on a template with bash, sed, and or awk (

rignes 02-15-2006 12:58 PM

Replacing a place holder on a template with bash, sed, and or awk
I'm trying to write up a script that will generate an html file based on a template. Specifically, the template has a place holder called <!--STYLE--> that I want to replace with text from a css file. The css is formatted with comments has tabs, spaces, and newlines (all the things used to make it not appear on one line).

What is the best way to do such a thing using only combination of bash, sed, and/or awk? perl may be better but I know nothing about it yet.

Thanks for you comments. ;)

schneidz 02-15-2006 01:55 PM

man grep for flags -B and -A

you can cat the css in between the two greps

does that make sense.

rignes 02-15-2006 06:12 PM

I'm sorry, it doesn't make sense. I'm not terribly experienced with scripting (yet). Perhaps if I give a specific example it would help.

I have a style sheet in a file called gal.css. The template file is just html. In this specific case, I want to replace the text <!--STYLE-->, which won't necessarily start at a specific line depending on future modifications of the template with the contents of the gal.css file. For readability I want to keep all the tabs and other white space. Also, I want to be able to include css comments which have the \ and * special characters.

My problem is, I'm not 100% what tool is best to use between sed or awk, or how best to use the specific tool. I'm not looking for a gimme answer here, a good hint and point in the right direction is welcome though.


chrism01 02-15-2006 06:57 PM

What I'd do is write a short Perl script that reads in the template file and copies it out to a new filename until it reaches the <!--STYLE--> marker, at which pt it inserts the css file, then continues with the template file.
This would be nicely extensible ie you could do other arbitrary amendments / insertions / deletes etc.

rignes 02-15-2006 07:24 PM

That's the problem, I don't know perl. I heardly know anything about sed or awk either. But the little that I do know is more than the big nothing I know about perl. :P Of course, perl is on the long long list of "things to learn someday". Until I get the time I was hoping to get sed or awk to do it.

Let's pretend for a moment that perl doesn't exist. How would you tackle this one? It may not be possible, and if that's the answer that's OK. Then atleast I know I need to curl up with a good book on perl.

chrism01 02-15-2006 09:56 PM

Actually, you could do the same (basic version) in bash, using file read while cmds and checking for <!--STYLE--> marker & then cat-ing the css file in, throw away <!--STYLE--> rec, then continue as before.
Bash is just less designed to do it and harder to add further customizations to.
It was originally really designed to automate the controlling/calling of other progs, rather than as a prog lang, although most things can be done from bash eventually....
Have a look at these links:
& I'd recommend getting a good book on your favourite shell (prob bash if Linux).
If you are going to be a serious programmer or admin for your company, I'd definitely advise learning a more poweful lang eg Perl (my pref), / Python etc soon-ish.
PS Come back if you have more qns

rignes 02-16-2006 02:14 PM

Your suggestions to use while read loops lead me to come up with this:


        while read; do
                if echo $REPLY | grep -q '<!--STYLE-->'; then
                        cat $libdir/gal.css
                        echo $REPLY

insert_css < template.html

This works, though I know it's not the best way it will hold me over until I learn some perl.


bigearsbilly 02-16-2006 04:20 PM

it's a bit long ;)

#!/usr/bin/perl -wp

s/<!--STYLE-->/`cat css.file`/e;

All times are GMT -5. The time now is 11:51 AM.