LinuxQuestions.org
Register a domain and help support LQ
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 08-26-2003, 08:14 AM   #1
player_2
Member
 
Registered: Aug 2003
Posts: 57

Rep: Reputation: 15
Arrow sed/awk problem


Alright, I'm completely new when it comes to sed and awk, and the tutorials I have found have done me no good. I need to edit my httpd.conf file in a script. In the httpd.conf file are two templates, one for Listen directives, the other for VirtualHost directives. The templates are marked off at the bottom of their sections (for example, all the Listen directives are grouped together, with the template at the bottom). Here's what the httpd.conf looks like:

Code:
# The last actual Listen directive

# sitename.dom
Listen 24.xxx.xx.xxx:80

##BEGIN_LISTEN_TEMPLATE##
## ##DOMAIN##
#Listen ##IPADDR##:##PORT##
##END_LISTEN_TEMPLATE##

#More configuration stuff follows... down to the VHosts

<VirtualHost 24.xxx.xx.xxx>
  SiteName sitename.dom
  etc...
</VirtualHost>

##BEGIN_VHOST_TEMPLATE##
#<VirtualHost ##IPADDR##>
#  SiteName ##DOMAIN##
#  etc...
#</VirtualHost>
##END_VHOST_TEMPLATE##
I need to take the contents of those templates, remove the leading #, and fill in the fields specified. Anyone know how I could do this with sed or awk? Thanks.

Last edited by player_2; 08-26-2003 at 08:31 AM.
 
Old 08-26-2003, 11:06 AM   #2
Blinker_Fluid
Member
 
Registered: Jul 2003
Location: Clinging to my guns and religion.
Posts: 682

Rep: Reputation: 63
Just wondering why you want to use sed or awk to do this... Seems to me like vi would be a lot easier and acurate to use...
If you had a lot of machines to do that were similar you could create a sample file that was the way you wanted and contained special fields that were unique in the file...
just an example the file might contain something like

hostname: HOSTNAME_OF_HOST
ip: IP_OF_HOST

where HOSTNAME_OF_HOST and IP_OF_HOST is something that only occurs where you want to replace it in the file
then the sed command would be something like
sed 's/HOSTNAME_OF_HOST/desired_hostname.com/g'
and for the ip
sed 's/IP_OF_HOST/192.168.0.123/g'

As far as removing the leading # signs you might want to be careful with that because comment fields might be uncommented where you don't want them. It is easy enought to remove them with sed 's/^#//g' but I would be cautious with it... (The ^ indicates begining of line)
 
Old 08-26-2003, 12:01 PM   #3
player_2
Member
 
Registered: Aug 2003
Posts: 57

Original Poster
Rep: Reputation: 15
The problem is that this needs to be executed from a script, and Vim doesn't play well with stdio redirection.

And I don't have to worry about removing necessary # signs, because the script will only apply to what's within the ##BEGIN_*_TEMPLATE## and ##END_*_TEMPLATE## blocks, and all lines (including comments) will have a # at the beginning, even if that means double-#ing comment lines.

The following does not work, and I can't figure out why:

Code:
awk '/^##BEGIN_VHOST_TEMPLATE##$/,/^##END_VHOST_TEMPLATE##$/p' < /usr/local/apache/conf/httpd.conf
It only prints out "##BEGIN_VHOST_TEMPLATE##", not the entire thing.
 
Old 08-26-2003, 12:03 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Though sed is probably not the best tool for this (perl, python, ruby or even awk is better), but it is possible and the only one of these I know something about...

So here a shell script using sed that gets you started (I hope):
Code:
#!/bin/bash

# edit/generate var's below to suit your needs

TEMPLATE_CONF="$HOME/dummy/httpd.conf.template"
CONF="$HOME/dummy/httpd.conf"

LISTEN_IPADDR="192.168.1.1"
LISTEN_PORT="80"
VHOST_IPADDR="192.168.2.2"
VHOST_SITENAME="mysite.com"

# end of editable var's

SED1="/^##BEGIN_LISTEN_TEMPLATE##/,/^##END_LISTEN_TEMPLATE##/ {"
SED2="s/^#\( *Listen *\)##IPADDR##:##PORT## */\1$LISTEN_IPADDR:$LISTEN_PORT/"
SED3="/^##BEGIN_LISTEN_TEMPLATE##/d"
SED4="/^##END_LISTEN_TEMPLATE##/d"
SED5="}"
SED6="/^##BEGIN_VHOST_TEMPLATE##/,/^##END_VHOST_TEMPLATE##/ {"
SED7="s/^#\( *<VirtualHost \)##IPADDR##> */\1$VHOST_IPADDR>/"
SED8="s/^#\( *SiteName \)##DOMAIN## */\1$VHOST_SITENAME/"
SED9="/^##BEGIN_VHOST_TEMPLATE##/d"
SEDA="/^##END_VHOST_TEMPLATE##/d"
SEDB="s/^#\(<\/VirtualHost>\)/\1/"
SEDC="}"

sed -e "$SED1" -e "$SED2" -e "$SED3" -e "$SED4" -e "$SED5" -e "$SED6" \
-e "$SED7" -e "$SED8" -e "$SED9" -e "$SEDA" -e "$SEDB" -e "$SEDC" \
"$TEMPLATE_CONF" >"$CONF"
 
Old 08-26-2003, 12:10 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by player_2
The following does not work, and I can't figure out why:
Code:
awk '/^##BEGIN_VHOST_TEMPLATE##$/,/^##END_VHOST_TEMPLATE##$/p' < /usr/local/apache/conf/httpd.conf
That's because it looks more like a sed script than a awk script.
This will work:
Code:
awk '/^##BEGIN_VHOST_TEMPLATE##$/,/^##END_VHOST_TEMPLATE##$/ {print}' /usr/local/apache/conf/httpd.conf
 
Old 08-26-2003, 02:02 PM   #6
Medievalist
Member
 
Registered: Aug 2003
Distribution: Dead Rat
Posts: 175

Rep: Reputation: 37
First of all, don't use awk to pull the template lines, use gawk. In most linux distros awk is a link to gawk, so that's no problem, but you might want to check. Gawk is significantly better and has been ported to many systems (including windows, VMS, and IBM mainframes).

Second, you are going to want to write a program for this (unless you'd rather have me do it - I do this for a living, though, so you'd have to pay me) so you should get yourself a copy of the gawk manual which is misleadingly named "Effective Awk Programming" written by Arnold Robbins (the gawk maintainer) and published by O'Reilly.

Gawk is fast and easy to learn, and you can knock out a program like this in 25 minutes or less with two weeks or so of practice. The first program will take you an hour if you are not an obsessive perfectionist, a couple of days if you are.
 
Old 08-26-2003, 02:18 PM   #7
player_2
Member
 
Registered: Aug 2003
Posts: 57

Original Poster
Rep: Reputation: 15
Thanks a lot guys...

Hko, won't that eliminate the template? I guess I'd have to copy it to a variable first, run that script, then rewrite it.

Yes, awk is a link to gawk on my system.
 
Old 08-26-2003, 02:49 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by player_2
Hko, won't that eliminate the template? I guess I'd have to copy it to a variable first, run that script, then rewrite it.
Do you mean if the original template file will be lost/overwritten/changed?
No, the script makes a (modified) copy.

The template file in the example script is $HOME/dummy/httpd.conf.template . The copy will be $HOME/dummy/httpd.conf

To try the script entirely save, make a directory "dummy" in your home directory ($HOME). Then copy the template file to this directory and rename it to "httpd.conf.template". Then copy-paste the script as I posted into a file in the same directory (e.g. "confedit.sh") and run it. After that you'll still have the file "httpd.conf.template" unchanged and a new file "httpd.conf".

You can change these filenames (and the directories they are in) by changing these lines of the script:
Code:
TEMPLATE_CONF="$HOME/dummy/httpd.conf.template"
CONF="$HOME/dummy/httpd.conf"

Last edited by Hko; 08-26-2003 at 02:52 PM.
 
Old 08-26-2003, 05:06 PM   #9
player_2
Member
 
Registered: Aug 2003
Posts: 57

Original Poster
Rep: Reputation: 15
Pardon me Hko, I looked at the s/... and the .../d and immediately thought "oh no".

The goal is to keep the templates inside httpd.conf.
 
Old 08-26-2003, 06:09 PM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
That's quite different from how I interpreted your question. Sorry about that.
Haven't got time at the moment to think about it the "new" way.
Maybe later.
 
  


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
SED, AWK or PERL HELP embsupafly Programming 6 08-20-2005 09:07 PM
Sed & Awk hinetvenkat Linux - Software 4 05-30-2005 05:10 AM
awk and sed issues alaios Linux - General 11 03-24-2005 05:33 AM
sed or awk help requested tonyfreeman Programming 7 10-03-2004 12:23 AM
awk/sed help pantera Programming 1 05-13-2004 11:59 PM


All times are GMT -5. The time now is 04:59 PM.

Main Menu
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