Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Sep 2017 20:55:00 -0400
From:      Ernie Luzar <>
To:        "" <>
Subject:   Help scripting dns lookup using awk 
Message-ID:  <>

Next in thread | Raw E-Mail | Index | Archive | Help
The following sh script works, but runs very slow.

   truncate -s 0 $host_out
   truncate -s 0 $host_error

# Make the input file read a line at a time, not a field at a time.
   set -f

   for line in `cat $host_in`; do
     domain_name=`echo -n $line | cut -w -f 1`
     host $domain_name > /dev/null

     if [ $? -ne 0 ]; then
       echo "$domain_name" >> $host_error
       echo "$domain_name" >> $host_out

The follow script uses awk trying to do the same thing.

   truncate -s 0 $host_out
   truncate -s 0 $host_error

   cat $host_in | awk '
     { system(host $1)
      rc_status = system($0)
      if (rc_status != 0)
         print $1 > $host_error
         print $1 > $host_out

# command line exec command.
 >hosts2void-dns_lookup.awk /tmp/aw.hosts \
/root/good.dns /root/bad.dns

# This is the output.
sh: not found
sh: not found
awk: illegal field $(), name "host_error"
  input record number 1, file
  source line number 5

I see 2 problems with my awk code.

1. The text output of the host command results is going
    the console screen. In the sh version I kill the output
    using > /dev/null  How would I to do something like that in awk.

2. I get that doing  print $1 > $host_error  is not allowed.
    What is the correct way to pass script variables to awk?

Now I am wondering if there is a simpler way to do dns lookup
in awk?

Want to link to this message? Use this URL: <>