Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info.

Basically my file looks like this:

...
<VirtualHost *:>
        ServerName server.name1.com
        ServerAlias svrname
        Redirect Permanent / http://www.sitename1.gov
# Disable HTTP TRACE Support (see http://www.kb.cert.org/vuls/id/867593)
        RewriteEngine On
        RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
        RewriteRule .* - [F]
</VirtualHost>

<VirtualHost *:>
        ServerName www.servername2
        ServerAlias srvname2
        ServerAlias srvname.2.com
        DocumentRoot /location/of/document/root
        DirectoryIndex index.shtml
        ScriptAlias /cgi-bin/ /tst/cgi-bin/
        ErrorDocument 403 http://www.error.com/errors/error403.html
#       ErrorDocument 404 http://www.error.com/errors/error404.html
# Disable HTTP TRACE Support (see http://www.kb.cert.org/vuls/id/867593)
        RewriteEngine On
        RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
        RewriteRule .* - [F]
</VirtualHost>
...

And I want it to look like this:

...
<VirtualHost *:>
                 ServerName server.name1.com
<VirtualHost *:>
                 ServerName www.servername2
                 DocumentRoot /location/of/document/root
...

There a simple or not so simple awk script for this? Thanks.

If you're on Solaris, use gawk, nawk or /usr/xpg4/bin/awk.

awk 'END {
    if (ok) print r
  }
/VirtualHost/ {
  if (ok) print r
  r = $0; ok = x  
  }
/ServerName|DocumentRoot/ { 
  r = r RS $0; ok++  
  }' infile
1 Like
grep -E 'VirtualHost|ServerName|DocumentRoot' *
awk '/<VirtualHost \*:>|ServerName|DocumentRoot/{print}' file
<VirtualHost *:>
        ServerName server.name1.com
<VirtualHost *:>
        ServerName www.servername2
        DocumentRoot /location/of/document/root

Works like a charm. Thanks a bunch!

The above command will output the lines containing the string VirtualHost regardless of the presence of ServerName and DocumentRoot directives.

Ooops you true.... i didn't see he wanted to discard those not having these ServerName and DocumentRoot