I have this "answer section" from a dig call:
www.google.com 300 IN A \<ip_address\>
I want to extract the sub-domain www out of the www.google.com to save in a variable.
I have tried to use sed to filter out the sub-domain name, then replace field one (i.e. $1), after passing to awk, with $host_name, like so:
host_name=$(echo get_dig "$@" | sed 's/..*//; s/ .*//')
...
get_dig "$@" | awk '{ $1 = $host_name; printf("The subdomain %s is a %s record and points to %s\\n", $1, $4, $5) }'
N.B: get_dig is a shell function I created to run dig and extract the "answer section".
I have tested and the sed part cuts as I expect it to and gives the correct result (i.e. www), but when I try to replace $1 with $host_name, it seems to alter the behaviour and returns the whole dig "answer section" as the hostname in the printf string. Also, I have $hostname declared globally.
PS: I have to use awk, but it's possible that I might have tunnel-visioned into a particular process, with sed
 
     
     
     
    