LinuxQuestions.org
Help answer threads with 0 replies.
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, 12: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, 01:55 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,308

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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, 06: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 06:51 PM.
 
Old 02-15-2006, 06:57 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,237

Rep: Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711
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, 07: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, 09:56 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,237

Rep: Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711
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, 02: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, 04:20 PM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,494

Rep: Reputation: 235Reputation: 235Reputation: 235
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 10:47 AM
SED, AWK or PERL HELP embsupafly Programming 6 08-20-2005 09:07 PM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 02:55 AM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 03:22 AM
awk/sed help pantera Programming 1 05-13-2004 11:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:36 AM.

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
Open Source Consulting | Domain Registration