LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 04-11-2013, 03:35 PM   #1
fizzistuff
LQ Newbie
 
Registered: Apr 2013
Posts: 1

Rep: Reputation: Disabled
Add missing ServerName directive to httpd.conf file using sed


I have the following virtual hosts in httpd.conf file

Code:
.... other lines of httpd.conf

    <VirtualHost *:80>
    ## could be comments
    could be any line
    could be any line
    could be any line
    </VirtualHost>

    <VirtualHost *:80>
       could be any line
    ## could be comments
    could be any line
    could be any line
    could be any line
    could be any line
    could be any line
    </VirtualHost>

    <VirtualHost *:80>
## may have comments
    ServerName ppp.com
        could be any line
    could be any line
    could be any line

    <VirtualHost *:443>
   ## could be empty
   could be any line
   could be any line
    ServerName zzz.com
    could be any line
    could be any line
    could be any line
    </VirtualHost>

.... other lines of httpd.conf
I'm trying to add "ServerName abc.com" to every virtual host which doesn't have a ServerName already setup.

I was trying to do it in sed, and I'm not getting anywhere .. any help ?

This is what I have so far ...

Code:
sed '/^[ \t]*<VirtualHost/,/^[ \t]*<\/VirtualHost/{
/^ServerName/!{
   /<VirtualHost/{
     /^/a\ServerName abc.com

   }
  }
}' httpd.conf
 
Old 04-11-2013, 09:09 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
[QUOTE=fizzistuff;4929964]Your sample input file contains this ...
Code:
    <VirtualHost *:80>
## may have comments
    ServerName ppp.com
        could be any line
    could be any line
    could be any line
Should this group of records terminate with </VirtualHost> just as the others do?

Daniel B. Martin
 
Old 04-11-2013, 09:37 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by fizzistuff View Post
I was trying to do it in sed, and I'm not getting anywhere .. any help ?
I find it easier to perform this transformation in small steps.
1) Reformat the input file so each "<VirtualHost *:80>" block of lines becomes one line.
2) If any line lacks a ServerName, plug in the default.
3) Reformat the file to look like the original.

With this InFile ...
Code:
    <VirtualHost *:80>
    ## could be comments
    could be any line
    could be any line
    could be any line
    </VirtualHost>

    <VirtualHost *:80>
       could be any line
    ## could be comments
    could be any line
    could be any line
    could be any line
    could be any line
    could be any line
    </VirtualHost>

    <VirtualHost *:80>
## may have comments
    ServerName ppp.com
        could be any line
    could be any line
    could be any line
    </VirtualHost>

    <VirtualHost *:443>
   ## could be empty
   could be any line
   could be any line
    ServerName zzz.com
    could be any line
    could be any line
    could be any line
    </VirtualHost>
... this code ...
Code:
 sed 's/$/~/' $InFile                             \
|paste -d"\0 " -s                                 \
|sed 's|</VirtualHost>~|</VirtualHost>\n|g'       \
|awk '{if ($0~"<VirtualHost" && $0!~"ServerName")  
   sub(">",">~     ServerName ABC.COM")} {print}' \
|sed 's/~/\n/g'                                   \
> $OutFile
...produced this OutFile ...
Code:
   <VirtualHost *:80>
     ServerName ABC.COM
    ## could be comments
     could be any line
    could be any line
     could be any line
    </VirtualHost>
 
    <VirtualHost *:80>
     ServerName ABC.COM
        could be any line
    ## could be comments
     could be any line
    could be any line
     could be any line
    could be any line
     could be any line
    </VirtualHost>
 
    <VirtualHost *:80>
 ## may have comments
    ServerName ppp.com
         could be any line
    could be any line
     could be any line
    </VirtualHost>
 
    <VirtualHost *:443>
    ## could be empty
   could be any line
    could be any line
    ServerName zzz.com
     could be any line
    could be any line
     could be any line
    </VirtualHost>
Daniel B. Martin
 
  


Reply



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
[SOLVED] search for specific <Directory> directive in httpd.conf and then append line bradvan Programming 6 12-20-2012 08:22 AM
How to configure /etc/httpd/conf/httpd.conf file to run a html on web arunava.saha Red Hat 2 05-30-2012 03:28 AM
Starting httpd: httpd: Syntax error on line 209 of /etc/httpd/conf/httpd.conf: Syntax sethukpathi Linux - Networking 6 04-12-2008 11:26 AM
PHP - What to add to Apache httpd.conf file? maktos Linux - Software 9 04-17-2004 08:18 PM
saving changes to apache2/conf/httpd.conf file Zaius Linux - Newbie 6 01-09-2004 11:45 PM

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

All times are GMT -5. The time now is 08:51 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