Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 10 Sep 2017 09:32:02 -0400
From:      Ernie Luzar <luzar722@gmail.com>
To:        "freebsd-questions@freebsd.org" <freebsd-questions@freebsd.org>
Subject:   awk help
Message-ID:  <59B53ED2.3000409@gmail.com>

Next in thread | Raw E-Mail | Index | Archive | Help
Hello list;

I wrote the following script to read a hosts file I downloaded
containing known bad sex domain names intended for addition to
/etc/hosts file. This script drops some useless records and builds
a local-zone: record for input to unbound. The file contains 55,000
records and this script takes 7 minutes to process it.

I know awk can do this same thing must faster. I have searched awk
and can not find any reference that would do what the tr '\r' ' '`
command does.

Would some one point me to documentation with example of how to get
the same result with awk as the tr '\r' ' '` command does.

Thanks.



#! /bin/sh
date
   host_out="$1"
   host_in="$2"
   truncate -s 0 $host_out

# Make the input file read a line at a time, not a field at a time.
   IFS=$'\n'
   set -f

   for line in `cat $host_in`; do

     # Locate and replace carriage return with blank.
     line=`echo -n "${line}" | tr '\r' ' '`

     # Locate and replace tab with blank.
     line=`echo -n "${line}" | tr '\t' ' '`

     # Drop blank lines.
     blank_line=`echo -n $line | cut -c 1-1`
     if [ "$blank_line" = " " ]; then
       continue
     fi

     # Drop lines with localhost in it.
     localhost=`echo -n $line | cut -w -f 2`
     if [ "$localhost" = "localhost" ]; then
       continue
     fi

     # Drop line with # in cloumn 1 as a comment.
     comment1=`echo -n $line | cut -c 1-1`
      if [ "$comment1" = "#" ]; then
       continue
     fi

     # Drop line with word Malvertising starting in cloumn 1
     comment1=`echo -n $line | cut -w -f 1`
     if [ "$comment1" = "Malvertising" ]; then
       continue
     fi

     # 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

   done
   date
   exit 0



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?59B53ED2.3000409>