Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 18 Apr 2017 21:09:17 +0200
From:      Andreas Perstinger <andipersti@gmail.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: awk help
Message-ID:  <b7c0da1d-e659-1430-9530-37993f9182b3@gmail.com>
In-Reply-To: <aed3ad4b-7013-471f-8b11-bc717230cff0@gmail.com>
References:  <58F25A01.1060208@gmail.com> <7951DF71-5CD3-4B53-9CB4-13CAA8945983@huiekin.org> <58F4CD14.7090008@gmail.com> <c95e03d2-986d-3c3c-198a-a28ab862dc70@gmail.com> <58F53EEA.2030206@gmail.com> <7b381f8f-e2a5-26ea-075e-96ae35efb25d@rogers.com> <58F61027.3090100@gmail.com> <aed3ad4b-7013-471f-8b11-bc717230cff0@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
(Sorry for messing up parts of the quoting in my former mail.)

On 2017-04-18 19:40, Andreas Perstinger wrote:
> I think awk is the better tool for your task but you could still
> simplify your shell script a little bit:

After hitting the send button I realized that there is a simpler 
solution using a classical Unix pipe:

#!/bin/sh

added_date="`date +%Y%m%d`"

hits_rpt="hits_rpt"
hits_new="hits.yes"
hits_no="hits.no"
truncate -s 0 $hits_rpt $hits_new $hits_no

ippool -l -d -m probing_ips > $hits_rpt 2> /dev/null

tail -n +4 $hits_rpt |          # start at 4th line
paste - - |                     # join two consecutive lines
sed -e 's:^ *::' -e 's:/32::' | # remove spaces at the beginning
                                 # and "/32" suffix from IP address
cut -w -f 2,4 |                 # extract IP and Hits from combined line
                                 # (at 2nd and 4th field)
while read ip hits              # read IP and Hits from each line
do                              # and do your work
     if [ "$hits" -gt 0 ]; then
         echo "$added_date  ${ip};" >> $hits_new
     fi
     echo "$hits  ${ip};" >> $hits_no
done
exit 0

If the "ippool" uses tabs in the output just add "tr '\t' ' '" between 
the "paste" and "sed" step.

Bye, Andreas



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?b7c0da1d-e659-1430-9530-37993f9182b3>