Comparing wc outputs using array

Hi All,

Im trying to compare the

wc -l  

output with another set of rowcount outputs which returned from sql...

For Eg : Im storing the first outputs as below

<srccnt=`wc -l $HOME/*.csv | awk {'print $1'}`

and comparing this with the another set of outputs.

descnt=`seclect count(*) from tables..

I have to do this comparison for each counts.

for eg,

srccnt  

retunrs 10 rows and

descnt 

returns another 10 rows.. I have to compare if each row is matching or not.

Can you help this with script.

I have tried this but not returning any outputs

 
testcount(){
if [ "$srccnt" == "$descnt" ]; then
echo "Count Matches">>$LOG
retval=0
else
echo "MISMATCH">>$LOG
retval=1
fi
}

I'm not surprised that you don't see any output as you redirected that to a log file.

Wildly guessing from the scenario that you present, I infer that you want to check if the rows in several DB tables match their counterpart .csv files? You can't do that in one large lump - do it table/file pair by table/file pair.

Thanks for the reply Rudic,

Yes, I have directed the o/p to log file. but in that log file,it simply writes all the wordcounts and row counts . doesn't do any matching comparison

 
Srccnt :
 715
441
232
273
2455
201
869
372
201
30652
339
224
1465
38439
descnt
 715
441
232
273
2455
201
869
372
201
30652
339
224
1465
38439

Can you help me in starting up with table match as you said..
Many thanks in advance

---------- Post updated at 06:24 AM ---------- Previous update was at 06:20 AM ----------

Can I store both the values in array and do comparison. could you guide me on this

That depends on the shell you use, which you fail to mention. In e.g. bash , sth like

srccnt=( $(wc -l $HOME/*.csv | awk {'print $1'}) )

will create the array srccnt , of which you can get at the element count with ${#srccnt�@]} .
Please provide way more details, e.g. file & table names.

EDIT: Sorry for the typo (my keyboard must have fooled me). Use ${#srccnt[@]} for the element count.

im using bash, RudiC
below are some of the files along with wordcounts

 715 Notes.csv
    441 Report.csv
    232 Client.csv
    273 Log.csv
   2455 Indications.csv
       869 Note.csv
      30652 Tasks.csv
    339 Team Notes.csv
      1465 Users.csv

table names are similar except .csv at end

is this the right way to call values in array

 
#srccnt�@]}

Assuming the 'select from table' example actualy reads out a table, it still compares the whole list/string with the number of lines from scrcnt.
At its best, you'd get a single "Mismatch" into your logfile.

Basic saying:
Try looping through each list element and compare it witch each table element.

Like in:

while read item
do   grep "$item" descnt.csv && \
        echo "$item: MATCH" >> "$LOG" || \
        echo "$item: MISmatch >> "$LOG"
done<srccnt.csv

EDIT:
Or as i see with multiple srcnt files:

for SRC in *csv
do
    while read item
    do   grep "$item" descnt.csv && \
            echo "$item: MATCH" >> "$LOG" || \
            echo "$item: MISmatch >> "$LOG"
    done<$SRC
done

hth

1 Like

Sorry for the typo, use ${#srccnt[@]} for the element count, c.f. man bash .

I'd propose you get a file with row counts and tables from SQL and then perform sth like

while read CNT TABLE
   do LCNT=$(wc -l < $TABLE.csv)
      ... comparison...
   done < file
1 Like

Many Thanks RudiC. the logic you suggested helped me..