Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nested while loop problem

Status
Not open for further replies.

MHThomas

Technical User
Nov 25, 2002
26
GB
Hello,
I hope somebody can point out the error of my ways. I am trying to do a simple counting program reads through a list of domains (stored in a flat file referenced as $list) copies relevant lines from a huge logfile ($inp) to a temporary file ($tmp), produces a unique list of these elements($tmp2), reads through the unique file, line-by-line counting the number of repeats, and then repeats the whole thing with the next domain from $list.
I know it's "clunky", but it's the only way I could think of doing it (with my limited programming experience).

#!/usr/bin/ksh
####################################################
# Read a list of domains, scan the specified logs, #
# produce a list of the number of occurences, sort #
# by number of occurences. #
################################################MHT#
#
# Set initial variables
#
list=/sd/csmlist.txt
out=/frep/csmlist.log
tmp=/tmp/csmlist.tmp
tmp2=/tmp/csmlist2.tmp
inp=/maillennium/log/stats.2005022410

#
# start read loop
#
exec 0<$list
while read line
do
rm -f $tmp $tmp2
cat $inp | egrep -i '(fr=)(.*)('$line')( ra=)' | awk -F"fr=" '{print $2}' | awk -F"@" '{print $1}' >> $tmp
sort -u $tmp >> $tmp2
exec 0<$tmp2
while read uni
do
noe=`cat $tmp |grep $uni | wc -l`
echo $line " " $uni " " $noe
echo $line " " $uni " " $noe >> $out
done
echo "$line done"
done

#
# Exit processing
#
exit


The problem is that it partially works; it reads the first domain, finds the ID's that have been used and counts the number of times (as expected), but one it has finished with the first domain, and exits the 'inside' while loop, it also exits the 'outside' loop (apparently cleanly, as if it had read the last record).
I'm assuming this problem is do to with nesting while loops, but I'm not sure.
Any ideas?

Cheers,

Mark T.

 
Instead of this:
exec 0<$list
while read line
do
...
exec 0<$tmp2
while read uni
do
...
done
...
done
You may consider this:
while read line
do
...
while read uni
do
...
done<$tmp2
...
done<$list

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top