awk help
Ernie Luzar
luzar722 at gmail.com
Tue Apr 18 13:09:58 UTC 2017
Mike Jeays wrote:
> snip
> That is an amazing difference in performance - I might have expected a
> five to ten times improvement, but not 300+ times.
> I don't see anything very time-consuming in the script above. Is it
> possible for you to post the equivalent csh and awk scripts? Either I or
> someone with more experience with csh might be able to spot the problem.
Here are the 2 scripts
#! /bin/sh
# This script is designed to process the o/p from the 2 line hit count
# Make the input file read a line at a time, not a field at a time.
IFS=$'\n'
set -f
added_date="`date +%Y%m%d`"
hits_rpt="/etc/ipf_pool_hits_rpt"
rm $hits_rpt
touch $hits_rpt
hits_new="/etc/ipf_pool.hits.yes"
rm $hits_new
touch $hits_new
hits_no="/etc/ipf_pool.hits.no"
rm $hits_no
touch $hits_no
ippool -l -d -m probing_ips > $hits_rpt 2> /dev/null
for line in `cat $hits_rpt`; do
# drop the first 3 rpt lines
poollist_line=""
poollist_line=`echo -n $line | grep poollist`
[ -n "${poollist_line}" ] && continue
role_line=""
role_line=`echo -n $line | grep Role`
[ -n "${role_line}" ] && continue
nodes_line=""
nodes_line=`echo -n $line | grep Nodes`
[ -n "${nodes_line}" ] && continue
in_line1=`echo -n $line | grep Address:`
[ -n "${in_line1}" ] && save_in_line1="${in_line1}"
in_line2=`echo -n $line | grep Hits`
[ -n "${in_line2}" ] && save_in_line2="${in_line2}"
if [ "${save_in_line1}" -a "${save_in_line2}" ]; then
build_line1=${save_in_line1##*:}
build_line1=${build_line1%%/*}
build_line1="${build_line1};"
build_line2=${save_in_line2##*Hits }
# So remove everything to the right of the word Bytes.
build_line2=${build_line2%%Bytes*}
if [ ${build_line2} -gt 0 ]; then
db_rec="$added_date ${build_line1}"
echo "${db_rec}" >> $hits_new
fi
build_line="${build_line2} ${build_line1}"
echo "${build_line}" >> $hits_no
in_line1=""
in_line2=""
save_in_line1=""
save_in_line2=""
else
continue
fi
done
exit 0
#! /bin/sh
hits_rpt="/etc/ipf_pool_hits"
# ippool -l -d -m probing_ips > $hits_rpt 2> /dev/null
awk 'BEGIN { "date +%Y%m%d" | getline date
hits_yes = "/etc/ipf_pool_awk_hits_yes"
hits_no = "/etc/ipf_pool_awk_hits_no"
system("rm " hits_yes)
system("touch " hits_yes)
system("rm " hits_no)
system("touch " hits_no)
}
/Address/ { address = $2; sub("/32", ";", address); got_address
= 1; }
/Hits/ { if (got_address)
{ hits = $2;
if (hits > 0)
{ print date, " ", address > hits_yes;
got_address = 0;
}
else
{ print "hit no", hits, address > hits_no;
got_address = 0;
}
}
}' $hits_rpt
Note; the
ippool -l -d -m probing_ips > $hits_rpt 2> /dev/null
command is run in the first script and the file it creates in used as
input in the second script so both scripts process the same data. This
ippool command is not the source of the big time difference between the
2 scripts, where the first one takes 5 minutes and the second takes 1
second.
More information about the freebsd-questions
mailing list