0

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

Ivan
  • 6,188
  • 1
  • 16
  • 23
Koyejo
  • 13
  • 3

3 Answers3

1

You never need sed when you're using awk. I think this is what you're trying to do, using any awk:

echo 'www.google.com 300 IN A \<ip_address\>' |
awk '{
    sub(/\..*/,"",$1);
    printf "The subdomain %s is a %s record and points to %s\n", $1, $4, $5
}'
The subdomain www is a A record and points to \<ip_address\>

Replace the echo '...' with get_dig "$@" (which I obviously don't have).

As for why your script didn't work, see How do I use shell variables in an awk script? and get rid of the echo in front of get_dig in the pipe to sed.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Your answer worked great and produced the desired output. It saved me from having to use `sed` and keeping track of another variable, so thanks for that! I did a lot of reading into `awk` because of this, but somehow, I did not come across this `sub` function. Is there an `awk` documentation that you could refer me to, so I can read more on sub and the other `awk` functions. – Koyejo Jul 13 '23 at 11:46
  • Get the book Effective AWK Programming, 5th Edition, by Arnold Robbins but `sub()` is described in every awk man page, e.g. https://man7.org/linux/man-pages/man1/awk.1p.html, so I'm not sure what documentation you were looking at that didn't have it but get rid of that documentation. It's also described in the POSIX awk spec, https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html. – Ed Morton Jul 13 '23 at 12:20
  • **after you buy the book**, you can also use the associated reference material at https://www.gnu.org/software/gawk/manual/gawk.html#String-Functions. – Ed Morton Jul 13 '23 at 12:22
0
awk '{ $1 = $host_name; printf("The subdomain %s is a %s record and points to %s\\n", $1, $4, $5) }'

You are attempting to access host_nameth field of line, if you wish to use shell variable inside awk command you should ram it using one of

-v var=val
--assign var=val 

Consider following simple example

host_name="www"
echo "uno dos tres" | awk -v host_name="$host_name" '{$1=host_name; printf("Subdomain %s is %s and points to %s\n",$1,$2,$3)}'

gives output

Subdomain www is dos and points to tres

Note: I use echo "uno dos tres" as stand-in for your command.

(tested in GNU Awk 5.1.0)

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

if you just wanna extract fields out of dig's response, here's a quick way to split it out. The formatting is trivial from here on :

dig +noall +answer www.google.com | 
                                    gtee >( gcat -n >&2; ) | 

     1  www.google.com.     112 IN  A   142.250.80.36

mawk NF=NF FS='[.][^ \t]+[ \t]+|[ \t].+[ \t]' OFS='\f'

www
   112
      142.250.80.36
RARE Kpop Manifesto
  • 2,453
  • 3
  • 11