Replacing a place holder on a template with bash, sed, and or awk
ProgrammingThis 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.
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.
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.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.