From owner-freebsd-questions@freebsd.org Sun Sep 10 19:00:25 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 D6D32E21778 for ; Sun, 10 Sep 2017 19:00:25 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mailrelay12.qsc.de (mailrelay12.qsc.de [212.99.163.153]) (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 5DAC282536 for ; Sun, 10 Sep 2017 19:00:25 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de ([213.148.129.14]) by mailrelay12.qsc.de; Sun, 10 Sep 2017 21:00:17 +0200 Received: from r56.edvax.de (port-92-195-85-107.dynamic.qsc.de [92.195.85.107]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx01.qsc.de (Postfix) with ESMTPS id 72CAD3CC3F; Sun, 10 Sep 2017 21:00:16 +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 v8AJ0FNY002117; Sun, 10 Sep 2017 21:00:15 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Sun, 10 Sep 2017 21:00:15 +0200 From: Polytropon To: Ernie Luzar Cc: "freebsd-questions@freebsd.org" Subject: Re: awk help Message-Id: <20170910210015.eaee44cd.freebsd@edvax.de> In-Reply-To: <59B53ED2.3000409@gmail.com> References: <59B53ED2.3000409@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 mailrelay12.qsc.de with AFF1D7A3D27 X-cloud-security-connect: mx01.qsc.de[213.148.129.14], TLS=1, IP=213.148.129.14 X-cloud-security: scantime:.1391 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: Sun, 10 Sep 2017 19:00:26 -0000 Allow me a few comments regarding sh -> awk for reformatting your input data. On Sun, 10 Sep 2017 09:32:02 -0400, Ernie Luzar wrote: > # Locate and replace carriage return with blank. > line=`echo -n "${line}" | tr '\r' ' '` Drop the ^Ms in a pipe step ... | tr -d '\r' | ... > # Locate and replace tab with blank. > line=`echo -n "${line}" | tr '\t' ' '` No need, awk defaults to tab(s) and/or space(s) as field separators, and you can easily access the fields with $1, $2, $3 and so on. > # Drop blank lines. > blank_line=`echo -n $line | cut -c 1-1` > if [ "$blank_line" = " " ]; then > continue > fi Just add a rule (length > 0) infront of your { ... awk statements for each line }. > # Drop lines with localhost in it. > localhost=`echo -n $line | cut -w -f 2` > if [ "$localhost" = "localhost" ]; then > continue > fi Expand the rule like (length > 0 && $2 != "localhost") { ... }, in case "localhost" is the exact text; if you want to use a reges, use $2 != /localhost/ instead. > # Drop line with # in cloumn 1 as a comment. > comment1=`echo -n $line | cut -c 1-1` > if [ "$comment1" = "#" ]; then > continue > fi Add another rule as reges !/^#/ && ( ... as above ... ) { ... } to filter those. Or, also possible, use ... | grep -v "^#" | ... infront of awk. > # Drop line with word Malvertising starting in cloumn 1 > comment1=`echo -n $line | cut -w -f 1` > if [ "$comment1" = "Malvertising" ]; then > continue > fi See above. > # Out put record. > ip=`echo -n $line | cut -w -f 1` > $trace_on echo "ip = ${ip}" > if [ "$ip" = "127.0.0.1" -o "$ip" = "0.0.0.0" ]; then > domain_name=`echo -n $line | cut -w -f 2` > echo "local-zone: \"${domain_name}\" always_nxdomain" >> $host_out > continue > else > domain_name=`echo -n $line | cut -w -f 1` > echo "local-zone: \"${domain_name}\" always_nxdomain" >> $host_out > fi Construct an output statement as desired. Use variables instead of $1, $2, $3 if the whole things gets too complex, for example like this (not tested, just for illustration): #!/bin/sh cat input.txt | tr -d '\r' | awk ' !/^#/ && (length > 0) { ip = $1 host = $2 if (!(host == "localhost" || ip == "127.0.0.1" || ip == "0.0.0.0")) printf("local-zone: \"%s\" always_nxdomain\n", ip) else printf("local-zone: \"%s\" always_nxdomain\n", host) }' > output.txt That should be basically what you need. You now just have to combine the moving parts correctly. :-) -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...