Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created October 12, 2017 17:33
Show Gist options
  • Save jhyland87/f939e9cadfdcce0e6c1120d187a20cc3 to your computer and use it in GitHub Desktop.
Save jhyland87/f939e9cadfdcce0e6c1120d187a20cc3 to your computer and use it in GitHub Desktop.
#! /usr/local/bin/awk -f
# echo -e "Jane Doe\n123 Main Street\nAnywhere, SE 12345-6789\n\nJohn Smith\n456 Tree-lined Avenue\nSmallville, MW 98765-4321" | ./include-test.awk
#@include "/Users/jhyland/Documents/scripts/awk/utils.awk"
function resetVhost() {
vhost["server_name"] = ""
vhost["document_root"] = ""
vhost["error_log"] = ""
vhost["access_log"] = ""
vhost["cfg_file"] = ""
vhost["ip"] = ""
vhost["port"] = ""
vhost["cfg_line_begin"] = ""
vhost["cfg_line_end"] = ""
}
function showHost() {
format = "%-20s: %s\n"
printf format, "Server Name", vhost["server_name"]
printf "%-20s: %s (Lines %s-%s)\n", "Config File", vhost["cfg_file"], vhost["cfg_line_begin"], vhost["cfg_line_end"]
if ( vhost["ip"] )
printf format, "IP Address", vhost["ip"]
if ( vhost["port"] )
printf format, "Port", vhost["port"]
}
BEGIN {
#vhost["server_name"] = ""
#vhost["document_root"] = ""
#vhost["error_log"] = ""
#vhost["access_log"] = ""
#vhost["cfg_file"] = ""
}
BEGINFILE {
resetVhost()
vhost["cfg_file"] = FILENAME
}
{
if ( $1 == "<VirtualHost" ){
#printf "[%s] Line is a VirtualHost OPEN tag\t%s\n", NR, $1
match( $2, "(\*|[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}):([0-9]{2,4})", ip_port )
if ( ip_port[1] ) vhost["ip"] = ip_port[1]
if ( ip_port[2] ) vhost["port"] = ip_port[2]
vhost["cfg_line_begin"] = NR
}
if ( $1 == "</VirtualHost>" ){
#printf "[%s] Line is a VirtualHost CLOSE tag\t%s\n", NR, $1
vhost["cfg_line_end"] = NR
showHost()
}
}
ENDFILE {
}
END {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment