LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-15-2006, 01:58 PM   #1
rignes
Member
 
Registered: Mar 2003
Location: USA
Distribution: Slackware-current
Posts: 155

Rep: Reputation: 30
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.
 
Old 02-15-2006, 02:55 PM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,197

Rep: Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642
man grep for flags -B and -A

you can cat the css in between the two greps

does that make sense.
 
Old 02-15-2006, 07:12 PM   #3
rignes
Member
 
Registered: Mar 2003
Location: USA
Distribution: Slackware-current
Posts: 155

Original Poster
Rep: Reputation: 30
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.

Thanks.

Last edited by rignes; 02-15-2006 at 07:51 PM.
 
Old 02-15-2006, 07:57 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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.
 
Old 02-15-2006, 08:24 PM   #5
rignes
Member
 
Registered: Mar 2003
Location: USA
Distribution: Slackware-current
Posts: 155

Original Poster
Rep: Reputation: 30
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.
 
Old 02-15-2006, 10:56 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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:
http://rute.2038bug.com/index.html.gz
http://www.tldp.org/LDP/abs/html/index.html
http://www.faqs.org/docs/bashman/bashref.html#SEC_Top
& 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.
HTH
PS Come back if you have more qns
 
Old 02-16-2006, 03:14 PM   #7
rignes
Member
 
Registered: Mar 2003
Location: USA
Distribution: Slackware-current
Posts: 155

Original Poster
Rep: Reputation: 30
Your suggestions to use while read loops lead me to come up with this:

Code:
insert_css()
{
        while read; do
                if echo $REPLY | grep -q '<!--STYLE-->'; then
                        cat $libdir/gal.css
                else
                        echo $REPLY
                fi
        done
} 

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.

Thanks.
 
Old 02-16-2006, 05:20 PM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,312

Rep: Reputation: 175Reputation: 175
it's a bit long
Code:
#!/usr/bin/perl -wp

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 11:47 AM
SED, AWK or PERL HELP embsupafly Programming 6 08-20-2005 10:07 PM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 03:55 AM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 04:22 AM
awk/sed help pantera Programming 1 05-14-2004 12:59 AM


All times are GMT -5. The time now is 11:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration