Shell script to count number of ~ from each line and compare with next line

Hi,
I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each line having same number of "~"s. so i need to check each line is having same "~" or not . my script gives me output like
**output**

total =        2
linelength=       62
14
linelength=       62
28

my script

#!/bin/bash
DATA_DIR=/export/opt/rtrupld/autosys/scripts_old
data=`ls $DATA_DIR/count.txt`
#     count1=1
var=`wc -l < $data`
#var2=`expr $var + 1`
echo "total = $var"
#var3=14
tildacount=0
while read line
do
   count=0
   linelen=`echo "$line"|wc -c`
 
echo "linelength= $linelen"
   if [ $linelen -gt 0 ]
     then 
# echo $linelen
       until [ $count -ge $linelen ]
     do
         count=`expr  $count + 1 `
         char=`echo "$line"|cut -b"$count"`
         if [ "$char" = "~" ]
         then
            tildacount=`expr  $tildacount + 1`
 
     fi
 
     done
     echo $tildacount
  fi
       
      
 
done < $data
#echo $tildacount

----Actually Outpur Required

if line 1 have number of ~=14
line 2  = 14
the correct file else
if line1 =14
line2= 15

then should give an error msg like ~ count is not same as prevoius line.

Thanks & Regards
Ganesh

awk ' {         sum=0; 
        for(i=1; i<= length($0); i++) {if (substr($0,i,1)=="~") {sum++} }
        if(FNR==1) {oldsum=sum}
        else
        {
           if(oldsum!=sum) 
              {print "error in ", FILENAME, " at line ", FNR, " tilde count=",sum}
        }  
      }' filename

Hi Jim,
Thanks your script is working fine.But i have one more problem like. your script every time goes into else loop. eve if oldsum is not eqal to sum then also its giving an error in count. i am getting result as below.

output----

error in  INTELLECT~EUR~EOD~40140~2010-09-21 00:19:20~20100920~~~~~~~~~  at line    tilde count= 14 
error in  INTELLECT~EUR~EOD~40140~2010-09-21 00:19:20~20100920~~~~~~~~~  at line    tilde count= 14 

I need to cout number od tilda and if the count is not equal then it should give an error count are not equal. e.g

line 1 count= 14
line 2 count= 15

then it should give error msg like line 1 and 2 are not same also i dont want line to be print only count need.
also what is FNR i didnt get it. I am weak in awk script.
could you please reply me if possible. My Boss is shouting on me how much time you are taking to solve this problem.

Thanks & Regards,
Ganesh

Try that.

awk ' {         sum=0; 
        for(i=1; i<= length($0); i++) {if (substr($0,i,1)=="~") {sum++} }
        printf( "line %d tilde count=%d ", FNR, sum)
        if (FNR==1){ oldsum=sum; next}
        if(oldsum != sum) {print " error"; errcnt++ }
        else { print "" }
        END { print "total errors", errcnt }
      }' filename > testfile
      

It sounds like you are in a job where you need to pick a skillset fast.