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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-15-2006, 12:58 PM
|
#1
|
Member
Registered: Mar 2003
Location: USA
Distribution: Slackware-current
Posts: 155
Rep:
|
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. 
|
|
|
02-15-2006, 01:55 PM
|
#2
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
man grep for flags -B and -A
you can cat the css in between the two greps
does that make sense.
|
|
|
02-15-2006, 06:12 PM
|
#3
|
Member
Registered: Mar 2003
Location: USA
Distribution: Slackware-current
Posts: 155
Original Poster
Rep:
|
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 06:51 PM.
|
|
|
02-15-2006, 06:57 PM
|
#4
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
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.
|
|
|
02-15-2006, 07:24 PM
|
#5
|
Member
Registered: Mar 2003
Location: USA
Distribution: Slackware-current
Posts: 155
Original Poster
Rep:
|
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.
|
|
|
02-15-2006, 09:56 PM
|
#6
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
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
|
|
|
02-16-2006, 02:14 PM
|
#7
|
Member
Registered: Mar 2003
Location: USA
Distribution: Slackware-current
Posts: 155
Original Poster
Rep:
|
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.
|
|
|
02-16-2006, 04:20 PM
|
#8
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
it's a bit long
Code:
#!/usr/bin/perl -wp
s/<!--STYLE-->/`cat css.file`/e;
|
|
|
All times are GMT -5. The time now is 12:23 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|