I am new to unix(just a week into it), i have problem there are two files2|1019|0|12 2|1019|3|0 2|1021|0|2 2|1021|2|0 2|1022|4|52|1030|0|1 2|1030|5|0 2|1031|4|4and 2|1019|0|12 2|1019|3|10 2|1021|0|22 2|1021|2|0 2|1022|4|15one is an output file and another an input file If the values in column 2 match, I want to sum the values in column 3 and 4 of both lines, else just the sum of the values in column 3 and 4 in the unique line for both input and output and compare the values of the total(3rd and 4th) and if diffrent raise a message with the value of 2nd column for which the sum is not matchingTotal of 1019 151021 41022 91030 61031 8in inputTotal of 1019 251021 241022 19in outputExpected output Unequal total for 1019,1021,1022Note: The values in input and output are pipe(|) seperatedRan this scriptawk -F '|' '{Arr[$2]=Arr[$2]+$3+$4}END{ for(i in Arr)print amount for planId  i  is :Arr[i]}'on first file and got this outputamount for planId  is :0amount for planId 1019 is :12amount for planId 1021 is :4amount for planId 1022 is :9amount for planId 1030 is :6amount for planId 1031 is :8dont know why the first line comes amount for planId  is :0
Matching the data in input vs output file
shell;awk
Another awk approach:$ awk 'NR==FNR{a[$2]+=$3+$4; next} {b[$2]+=$3+$4;}       END{           for(i in a){             if(i in b && a[i]!=b[1]){                print Unequal total for,i             }           }       }' input output Unequal total for 1019Unequal total for 1021Unequal total for 1022Or, if you really need the exact output you show in your question:$ awk 'NR==FNR{a[$2]+=$3+$4; next} {b[$2]+=$3+$4;}       END{            for(i in a){                if(i in b && a[i]!=b[1]){                    c[i]                }            }             printf Unequal total for ;             for(i in c){printf %s, , i}        }' input output | perl -pe 's/,\s*$/\n/'Unequal total for 1019, 1021, 1022FNR is the current file's line number and NR is the overall line number of all input. The two are equal only while the first file is being read. So, NR==FNR{a[$2]+=$3+$4; next} will add the sum of the 3rd and 4th column, to the value associated with the 2nd column in the array a, and do so only for the first file. The next ensures that we move to the next line and don't execute the rest of the script for the current line. The {b[$2]+=$3+$4;} will only be run if the previous one wasn't, if we're reading the second file. It does the same thing for the values in the second file, except it saves them in the array b. Once we reach the end of all input, the END{} block is executed. This will iterate over all keys in a and, if they are also keys in b, and their value is not the same, it prints out the key. 