3

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.

<VirtualHost 192.168.1.10:80>
    SSLEngine on
    SSLCACertificateFile /usr/share/ssl/certs/ca-bundle.crt
    SuexecUserGroup anzenketh wheel
    ServerName     anzenketh.net
    ServerAlias    www.anzenketh.net
    ServerAdmin    [email protected]
    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
    </Directory>
    CustomLog      /var/log/httpd/anzenketh/anzenketh.net-access_log combined
    ErrorLog       /var/log/httpd/anzenketh/anzenketh.net-error_log
</VirtualHost>

4 Answers 4

3

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

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
1
  • Not quite what I am looking for. Does not fulfill the requirement of searching for the domain.
    – anzenketh
    Commented Sep 5, 2012 at 12:57
2
awk '/Iowa/,/Montana/' file

Replace string A and B to your own.

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

Replace all contents within [] with correct values

1
  • 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
    Commented Dec 29, 2014 at 14:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .