Compare sum of two columns if variance is zero do nothing else send an email

Welcome!

Please be VERY precise when phrasing your requests. Above script will NOT "work for any 2 sets of files". It will if file1 is structurally equivalent to Ariba.csv, and file2 to Pay1.csv.

Sorry, for the wrong context here.

By any, I meant, different sets which are created every day with a similar format and equivalent structure.

Also, I want the output to be displayed in a separate file. So I added :

awk'...' Ariba.csv FS=, Pay.csv > outputfile.csv

at the end of the line and it works.

But if I want this outputfile to have an output of first sum, output of 2nd sum and variance. Do I have to print these in here?

if (SUM) print "Variance is not zero: ", SUM/100

It's not that easy, it needs a slightly different approach: not count up and then down a single variable, but sum up first var, then second, and then print both and their difference.
Try and post here for discussion.

1 Like

So after trying and trying, I found playing with awk little hard :frowning:

So I tried a new approach.

here is the code...

#!/bin/bash/sh
a=0.000000
for value in `cat file1  | grep Total | tr -s " " " " | cut -d " " -f2`
do
a=$(echo $a + $value | bc)
done
echo "file1 : $a"
b=0.000000
for value2 in `cat file2 | cut -d "," -f7 | sed -e 's/^"//g' -e 's/"$//g'`
do
b=$(echo $b + $value2 | bc)
done
echo "file2 Total: $b"
if [ $a = $b ]
then
echo "Total is same"
fi

How about (untested)

awk '
FNR == 1        {next
                }
FNR == NR       {if (/^Total/) SUM1 += $2*100
                 next
                }
                {SUM2 += $7*100
                }
END             {print SUM1/100, SUM2/100, "Variance: ", (SUM2 - SUM1))/100
                }
' Ariba.csv Pay.csv

It works too.. but still the output is not in decimal.

I'm done with the code part and was able to add few more things..

I'll share my code here once i'm all done.. 5-10%of coding pending I can say.

I've a question:
My code as shared above(most recent one) is working fine but when the first row has the naming for the columns it doesn't work and throws syntax error.

Can you please help me with the command which elliminates/removes the first row of the file?
The file looks something like this:

PaymentId,InvoiceReconciliationId,PurchasingUnit,InvoiceNumber,InvoiceDate,Supplier,GrossAmount,GrossAmountCurrency,PaymentTerms,ForTaxAccrual,SupplierLocation,SupplierLocationContactID,RemittanceLocation,cus_ManualInvoiceNumber,cus_LocationCode_UniqueName,cus_EstimatePaymentDate,cus_CreateDate,cus_SupplierName,cus_PaymentDescription,cus_RequesterPIN
"PAYMXF5477-209123","IRMXF5477-209123",,"MXF5477","06/04/2018 05:09:00 -0700","5002362","110.6500","USD","Net 30","No","5002362","5002362","RL_INTERNAL50834041",,,,"Mon Jun 4 10:10:13 PDT 2018","CDW DIRECT LLC",,"1541"
"PAY81171-209097","IR81171-209097",,"81171","05/31/2018 09:23:00 -0700","VM1039342","5775.0000","USD","Net 00","No","VM1039342","VM1039342",,,,,"Mon Jun 4 09:24:22 PDT 2018","PAYSCALE INC",,"3661"
"PAYMXF5657-209126","IRMXF5657-209126",,"MXF5657","06/04/2018 05:11:00 -0700","5002362","24.9800","USD","Net 30","No","5002362","5002362","RL_INTERNAL50834092",,,,"Mon Jun 4 10:12:06 PDT 2018","CDW DIRECT LLC",,"7337"

Use 1d; in the sed command.

What does "the output is not in decimal" mean?

1 Like

Sorry for the late reply, I have been busy with multiple things rn.

Thanks for the help Rudic!

Here is what I used...
I didn't delete the complete Row from the file, instead I just removed the Column name.

if [ $string == 'GrossAmount' ]; then

echo "$string"

sed -i '1d' $filename

fi

You were a great help!
:slight_smile: :slight_smile: