From owner-freebsd-questions@freebsd.org Fri Sep 15 12:48:05 2017 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 51DB5E14116 for ; Fri, 15 Sep 2017 12:48:05 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mailrelay13.qsc.de (mailrelay13.qsc.de [212.99.187.253]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.antispameurope.com", Issuer "TeleSec ServerPass DE-2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id B981F65EEA for ; Fri, 15 Sep 2017 12:48:04 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de ([213.148.129.14]) by mailrelay13.qsc.de; Fri, 15 Sep 2017 14:47:49 +0200 Received: from r56.edvax.de (port-92-195-6-118.dynamic.qsc.de [92.195.6.118]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx01.qsc.de (Postfix) with ESMTPS id C2DB63CC3F; Fri, 15 Sep 2017 14:47:48 +0200 (CEST) Received: from r56.edvax.de (localhost [127.0.0.1]) by r56.edvax.de (8.14.5/8.14.5) with SMTP id v8FCllUr002167; Fri, 15 Sep 2017 14:47:47 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Fri, 15 Sep 2017 14:47:47 +0200 From: Polytropon To: Ernie Luzar Cc: "freebsd-questions@freebsd.org" Subject: Re: Help scripting dns lookup using awk Message-Id: <20170915144747.5dc31f69.freebsd@edvax.de> In-Reply-To: <59BB24E4.6060908@gmail.com> References: <59BB24E4.6060908@gmail.com> Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-cloud-security-sender: freebsd@edvax.de X-cloud-security-recipient: freebsd-questions@freebsd.org X-cloud-security-Virusscan: CLEAN X-cloud-security-disclaimer: This E-Mail was scanned by E-Mailservice on mailrelay13.qsc.de with 0CB0B683547 X-cloud-security-connect: mx01.qsc.de[213.148.129.14], TLS=1, IP=213.148.129.14 X-cloud-security: scantime:.1341 X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Sep 2017 12:48:05 -0000 On Thu, 14 Sep 2017 20:55:00 -0400, Ernie Luzar wrote: > host_in="$1" > host_out="$2" > host_error="$3" > 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 > else > 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: medrx.sensis.com.au: not found > sh: medrx.sensis.com.au: not found You're not providing the whole command as needed; system($0) will only try to execute the hostname, not a "host " command. > awk: illegal field $(), name "host_error" > input record number 1, file > source line number 5 The $ is a reserved character in awk to indicate the fields; $0 is the whole record, $1 the first field, and so on. > 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. Combine the command string before executing, for example like this: cmd = sprintf("/usr/bin/host %s > /dev/null 2>&1", $1) rc = system(cmd) This should suppress all messages, and you can still evaluate the return code of the external program call. > 2. I get that doing print $1 > $host_error is not allowed. > What is the correct way to pass script variables to awk? Look closely: Your awk script is in ' ... ' (single quotes). According to standard sh behavior, this means that $ is not expanded (unlike " ... $ ..."). If you want to transfer parameters into an awk script from a sh "enclosure", use awk's -v parameter. For example: ... | awk -v host_out=${host_out} -v host_error=${host_error} ' # your awk code here ' > Now I am wondering if there is a simpler way to do dns lookup > in awk? Just tidy up your code a little bit, the basic parts are already there. ;-) -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...