Comparing wc outputs using array


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Comparing wc outputs using array
# 1  
Old 04-21-2015
Comparing wc outputs using array

Hi All,

Im trying to compare the
Code:
wc -l

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

For Eg : Im storing the first outputs as below
Code:
<srccnt=`wc -l $HOME/*.csv | awk {'print $1'}`

and comparing this with the another set of outputs.
Code:
descnt=`seclect count(*) from tables..

I have to do this comparison for each counts.

for eg,
Code:
srccnt

retunrs 10 rows and
Code:
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

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


Last edited by Deena1984; 04-21-2015 at 08:17 AM..
# 2  
Old 04-21-2015
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.
# 3  
Old 04-21-2015
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

Code:
 
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
# 4  
Old 04-21-2015
That depends on the shell you use, which you fail to mention. In e.g. bash, sth like
Code:
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.

Last edited by RudiC; 04-21-2015 at 09:58 AM..
# 5  
Old 04-21-2015
im using bash, RudiC
below are some of the files along with wordcounts

Code:
 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
Code:
 
#srccnt™@]}

# 6  
Old 04-21-2015
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:
Code:
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:
Code:
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
This User Gave Thanks to sea For This Post:
# 7  
Old 04-21-2015
Quote:
Originally Posted by Deena1984
.
.
.

is this the right way to call values in array
Code:
 
#srccnt™@]}

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
Code:
while read CNT TABLE
   do LCNT=$(wc -l < $TABLE.csv)
      ... comparison...
   done < file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing files in a directory against an array of files

I hope I can explain this correctly. I am using Bash-4.2 for my shell. I have a group of file names held in an array. I want to compare the names in this array against the names of files currently present in a directory. If the file does not exist in the directory, that is not a problem.... (5 Replies)
Discussion started by: BudMan
5 Replies

2. UNIX for Dummies Questions & Answers

gunzip outputs gibberish

I run cygwin on windows 7, and have been using the windows command line. I've been trying to gunzip some previously compressed large sequence output files in .txt.gz format. This worked for about the first 10 files and then for the rest of them the file (as viewed using the 'head' command) is... (1 Reply)
Discussion started by: woceht
1 Replies

3. UNIX for Dummies Questions & Answers

Need help with 2 similar commands with different outputs

NEVERMIND.... "$TIME" did the trick... stupid me. :o Hi, I know that this must be something pretty dumb but I cant figure it out myself. I have this: #!/bin/bash TIME="Time: 20120611" FILE=file.log grep $TIME $FILE And I get the following output: file.log:Time: 20120611... (2 Replies)
Discussion started by: RedSpyder
2 Replies

4. Shell Programming and Scripting

Comparing two array in shell script

In shell script i have two variables with value debit_sal="DOG,HIU,JIU,GYT,PPI,KIU,HUY........." debit_req='HIU, JIU, HUY, GTR, KOI, ............" i stored this two in two array arr=$(echo $debit_sal| tr "," "\n"); arr1=$(echo $debit_req| tr ", " "\n"); #(note second arry has comma with... (5 Replies)
Discussion started by: greenworld123
5 Replies

5. Shell Programming and Scripting

Array in awk outputs multiple values

Disclaimer: OP is 100% Awk beginner. I use this code on ASCII files I need to report against. awk 'BEGIN { tokens = 0 tokens = 0 tokens = 0 } { for (token in tokens) { if ($1 == token){print $0; tokens++;}}} END {for (token in tokens){ if( tokens ==... (1 Reply)
Discussion started by: alan
1 Replies

6. Shell Programming and Scripting

create outputs from other command outputs

hi friends, The code: i=1 while do filename=`/usr/bin/ls -l| awk '{ print $9}'` echo $filename>>summary.csv #Gives the name of the file stored at column 9 count=`wc -l $filename | awk '{print $1}'` echo $count>>summary.csv #Gives just the count of lines of file "filename" i=`expr... (1 Reply)
Discussion started by: rajsharma
1 Replies

7. Shell Programming and Scripting

comparing variables in awk array

Hi, I am trying to sort and display the below(like) input using awk command: Input: ------ 0;A 4;A 5;A 33;A 45;A 0;B 4;B 5;B 33;B 45;B Output (desired): (5 Replies)
Discussion started by: pvamsikr
5 Replies

8. UNIX for Dummies Questions & Answers

Comparing two strings those are outputs of echo

Hello, I have two strings those are outputs of two echo commands: $ logslist=`ls -1rt vp_lb_indv*.log | tail -1`; tail -7 $logslist | head -1 ERROR: Errors printed on pages 5,22. $ logslist=`ls -1rt vp_lb_indv*.log | tail -1`; errpages=`tail -7 $logslist | head -1`; echo... (3 Replies)
Discussion started by: memoconq
3 Replies

9. Shell Programming and Scripting

compare 2 outputs

Hello, I have a shell script that is used as follows: ./script -s <Oracle_server_name> the script returns several lines in this format: parameter name required value Current Value comments --------------- ------------- -------------- --------- My objective now is to compare 2... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

10. Shell Programming and Scripting

Printing outputs using awk.

I have a output of a command like this. the command is : bdf|sed '/^e/d'|awk '{print$2/1048576}' output : 0 0.515625 0.481979 2 2 2 7.8125 4 2 0.488281 7.8125 3.90625 4 1.95312 1 0.488281 (4 Replies)
Discussion started by: Krrishv
4 Replies
Login or Register to Ask a Question