I am working on one shell script to find the specific records from data file and add the totals into variables and print them. you can find the sample data file below for more clarification.
In the above sample data file, I want to read all Records with word Records Sent and then get the totals and sums for all records together.
Out Put Should be like this
Total Records Sent: 100+200+300 = 600
Total Sum : 10+20+30 = 60
I tried the following code:
total = 0
sum = 0
grep "Records Sent:" filename > filename.tmp
cat filename.tmp |\
while read line
do
tot = `$line | cut -f3 -d " "`
su = `$line | cut -f5 -d " "`
total ='expr $total + $total`
sum =`expr $sum +su`
done
echo $total
echo $sum
I am having some issues while running this code. Those issues are
when i am using while read line so it will try to goby line by line but basically it should go by record by record because the Record which has Records Sent: will be 2 lines.
I had another error saying cannot create. I have no idea why this error is coming.
Can anybody write above script in different way.Please advise me on this
# awk '/Records Sent/{a[$1FS$2]+=$3;b[$4]+=$5}END{for(i in a)print v,i,a;for(i in b)print v,i,b}' v="Total" file
Total Records Sent: 600
Total Sum1: 60
I don't want to rewrite everything because I have lot other validations included in the same script. My below code is working for total but not for sum because I have some decimals in my sum.