ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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:
# The last actual Listen directive
#More configuration stuff follows... down to the VHosts
# SiteName ##DOMAIN##
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.
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
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
and for the ip
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)
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:
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.
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: