awk help

Andreas Perstinger andipersti at gmail.com
Tue Apr 18 19:09:21 UTC 2017


(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


More information about the freebsd-questions mailing list