Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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    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
    </Directory>
    CustomLog      /var/log/httpd/anzenketh/anzenketh.net-access_log combined
    ErrorLog       /var/log/httpd/anzenketh/anzenketh.net-error_log
</VirtualHost>
share|improve this question
add comment

3 Answers 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
share|improve this answer
    
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
add comment

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
share|improve this answer
    
Not quite what I am looking for. Does not fulfill the requirement of searching for the domain. –  anzenketh Sep 5 '12 at 12:57
add comment
awk '/Iowa/,/Montana/' file

Replace string A and B to your own.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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