I know that I can use the -A and -B command for grep to get a lot of what I am looking for however that is not quite what I want.

I am looking to parse the httpd.conf file to search for a domain. Then display everything between the VirtualHost tags for that domain. A example of the virtualhost is as follows.

To search for a domain I run the following command:

less /usr/local/apache2/conf/httpd.conf |grep domain.tld

But that does not give me the full virtualhost only the lines that contain the domain.

    SSLEngine on
    SSLCACertificateFile /usr/share/ssl/certs/ca-bundle.crt
    SuexecUserGroup anzenketh wheel
    ServerName     anzenketh.net
    ServerAlias    www.anzenketh.net
    ServerAdmin    root@anzenketh.net
    DocumentRoot   /home/anzenketh/www/anzenketh.net
    ScriptAlias    /cgi-bin/ "/home/anzenketh/www/cgi-bin/"
    <Directory /home/anzenketh/www/cgi-bin>
        AllowOverride None
        Options ExecCGI
        Order allow,deny
        Allow from all
    CustomLog      /var/log/httpd/anzenketh/anzenketh.net-access_log combined
    ErrorLog       /var/log/httpd/anzenketh/anzenketh.net-error_log

Here is a sed call to get everything between (and including) two specific strings:

sed -n '/<VirtualHost*/,/<\/VirtualHost>/p' httpd.conf

If you want to search for a specific domain, just add it:

sed -n '/<VirtualHost>*/,/<\/VirtualHost>/p' httpd.conf
  • Not quite what I am looking for. Does not fulfill the requirement of searching for the domain. – anzenketh Sep 5 '12 at 12:58
  • You can just add the domain you are searching for, I updated my answer. – scai Sep 5 '12 at 13:08
  • That will find the IP not the domain. – anzenketh Sep 5 '12 at 13:11

Must use grep? sed and awk are more suitable for such tasks:

sed -n '/<VirtualHost /,/<\/VirtualHost>/p' inputfile
awk '/<VirtualHost /,/<\/VirtualHost>/' inputfile

As httpd.conf directives are case-insensitive, you may prefer to use case-insensitive matching:

sed -n '/<VirtualHost /I,/<\/VirtualHost>/Ip' inputfile
gawk -vIGNORECASE=1 '/<VirtualHost /,/<\/VirtualHost>/' inputfile

(IGNORECASE is GNU extension, only available in gawk.)

Update according to the changed question:

sed -n '/<VirtualHost\s\+192\.168\.1\.10\b/I,/<\/VirtualHost>/Ip' inputfile
gawk -vIGNORECASE=1 '/<VirtualHost\s+192\.168\.1\.10\>/,/<\/VirtualHost>/' inputfile
  • Not quite what I am looking for. Does not fulfill the requirement of searching for the domain. – anzenketh Sep 5 '12 at 12:57
awk '/Iowa/,/Montana/' file

Replace string A and B to your own.

grep -Pzio '(?s)<VirtualHost ([yourserverip]|\*)(:(80|443))?>(?:.(?!</VirtualHost))*?ServerName +[yourservername](?:(?!</VirtualHost).)*?</VirtualHost>' [yourfilename]

Replace all contents within [] with correct values

  • You should strongly consider adding some explanation of your solution so that new users can learn from your example rather than just copying/pasting. – HalosGhost Dec 29 '14 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.