shell script for comparing two files

Hi,

I have 2 files as below

FILE1.dat

co1|co2|co3
abnd|45.56|AZ
dkny|30.2|PA
sam|.23|VA

FILE.CTL

FILENAME|FILE1.dat
NO OF RECORDS|3
CHECKSUM|75.99

Could you please help me to write a shell script to compare

  1. TOTAL RECORDS from FILE1.dat with NO of RECORDS in FILE.CTL
  2. SUM up co2 in FILE1.dat and compare with CHECKSUM value in FILE.CTL

If both conditions are true then success or else fail the script.

Please help

Thanks for your responces
-Deep

What have you tried?

I have tried like this, but could not able to figure out to solve the issue

 
#!/bin/ksh
set A=`cut -d "|" -f2 /home/user/FILE1.dat`
set E=`$A | awk "{sum +=$1} END { printf "%.2f", sum }"`
set B=`awk "END {print NR-1}" /home/user/FILE1.dat`
set C=`grep "NO OF RECORDS" /home/user/FILE.ctl | cut -d "|" -f2`
set D=`grep "CHECKSUM" /home/user/FILE.ctl | cut -d "|" -f2`
if ($E=$D and $B=$C) then
   echo Status: Success
   exit 0;
else
   echo Status: Failed
   exit 1;
fi

The first thing is to know how to count the records: At first look your file1.dat has 4 records, but in the controlfile it counts 3... is it to say it ignores the first record (being a heard?...)

sorry for not mentioning it before, yes the first column in FILE1.dat should be ignored

Something not correct here:

 E=`echo $A | awk '{sum +=$1} END { printf "%.2f", sum }'`

I had a bit of time to look in depth, so forget the previous post and look:

#the script:
n12:/home/vbe/wks/z/test01 $ vi test01
#!/bin/ksh
cut -d "|" -f2 FILE1.dat|grep -v co> A                # Your following awk needs an infile at the end so  
                                                      #created  A file to be removed at the end of execution
E=$( awk '{ sum +=$1 } END { printf "%.2f", sum }' A) #see now? and as mentionned in previous post single quote
  echo E $E
B=$(awk "END {print NR-1}" FILE1.dat )                # The rest was correct - just changed to more modern ksh
  echo B $B                                           # syntax  $( commands...)
C=$(grep "NO OF RECORDS" FILE.ctl | cut -d "|" -f2)
  echo C $C
D=$(grep "CHECKSUM" FILE.ctl | cut -d "|" -f2)
  echo D $D 

rm A 

if [ $E -eq $D -a  $B -eq $C ]                        # You are testing numeric values no?
then
   echo Status: Success
   exit 0
else
   echo Status: Failed
   exit 1
fi
#exit...
:q

# Execution output:
n12:/home/vbe/wks/z/test01 $ ./test01 
E 75.99
B 3
C 3
D 75.99
Status: Success
n12:/home/vbe/wks/z/test01 $ 
1 Like

when i tried the same in c shell i'm getting the 4 values as out put,
here i have modified the if condition..

if ("$E" ="$D" & "$B" = $"C" )
then
  echo Status: Success
  exit 0;
else
  echo Status: Failed
  exit 1;
fi

in the output its giving seperate line as

count.sh: line 30: 75.99 =75.99: command not found
count.sh: line 30: 3=3: command not found
status: failed

awk -F"|" '(FNR!=1&&NR==FNR){a[FNR]=$2+0;next}(FNR!=1){s+=$2}END{if(s~a[3]&&(FNR-1)==a[2]){print "Success"}else{print "Failed"}}' fil1.ctl file1