how to compare counts in two separate files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to compare counts in two separate files
# 1  
Old 07-12-2009
Error how to compare counts in two separate files

Hi all,

what will be the code to compare count present in two seperate files

for e.g file (a) contains counts

Code:
100

and file (b) contains records

Code:
90

since both these files have differnt count so it will display count didnt match

and in case of success it display

verified

Also if it fails it should skip my command and in case of succes it should run my predefined code

i hope u ppl will helpSmilie

thnkz in advanceSmilie

waiting desperately.Smilie
# 2  
Old 07-12-2009
I am not sure I understand your requirement completely, but is this what you are looking for ?

Code:
$ 
$ cat f1
100
$ cat f2
90
$ if [ $(cat f1) == $(cat f2) ]; then echo "Count matched"; else echo "Count didn't match"; fi
Count didn't match
$ cat>f2
100
^C
$ cat f1
100
$ cat f2
100
$ if [ $(cat f1) == $(cat f2) ]; then echo "Count matched"; else echo "Count didn't match"; fi
Count matched
$ 
$

tyler_durden
# 3  
Old 07-12-2009
thnkz durden_tyler,

But i am getting this error

here is the code
Code:
 
#!/bin/sh
cat bak_scp0.wri | grep 'rec20090711' | wc -l > a.wri
cat ser_scp0.wri | grep 'rec20090711' | wc -l > b.wri
if [ $(cat a.wri) == $(cat b.wri) ]; then echo "Count matched"; else echo "Count didn't match"; fi

: ==: 0403-012 A test command parameter is not valid.
Count didn't match
# 4  
Old 07-12-2009
if i get you right too...

Code:
#!/bin/ksh
f1=`wc -l < file1`
f2=`wc -l < file2`
if [ $f1 == $f2 ]
then
echo "Verified"
###Your predefined code###
else
echo "Count doesnt Match"
fi

# 5  
Old 07-12-2009
now i am getting error like

Code:
 
2640
2640
m.sh[12]: test: 0403-021 A ] character is missing.
Count didn't match

please also find the attached code

Code:
 
#!/bin/sh
cat bak_scp0.wri | grep 'rec20090711' | wc -l > a.wri
cat ser_scp0.wri | grep 'rec20090711' | wc -l > b.wri
f1=`cat a.wri`
f2=`cat b.wri`
echo $f1
echo $f2
if [$f1==$f2]
then
echo "Count matched"
else
echo "Count didn't match"
fi


please advice
# 6  
Old 07-12-2009
In this site has said this over nnn times.
[ is command, not bracket = very difficult for programmers.
On the command line must be delimeter between arguments, so
Code:
if    [    "$f1"   =    "$f2"  ]

test command (same as [ ) need 4 argument, if command has written using [, but if you use other test format "test", then it is
Code:
if test  "$f1" = "$f2"
then
   ...

test and [ are same builtin command, but if you use [ version of this command, then last argument must be ] - yes it's funny - done for programmers.

More about if and test

Shortly: put stdout to the variable, no need to use some temp file.
Code:
#!/bin/sh
f1=$(  cat bak_scp0.wri | grep 'rec20090711' | wc -l    )
f2=$(  cat ser_scp0.wri  | grep 'rec20090711' | wc -l    )
echo $f1
echo $f2
if [ "$f1" = "$f2" ]
then
   echo "Count matched"
else
   echo "Count didn't match"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to use diff output to compare to a separate file

I have two files: smw:/working/iso_testing # cat a QConvergeConsoleCLI-1.1.03-49.x86_64.rpm aaa_base-13.2+git20140911.61c1681-1.3.i586.rpm acpica-20140724-2.1.2.i586.rpm test.rpm smw:/working/iso_testing # cat b QConvergeConsoleCLI-1.1.03-49.x86_64.rpm... (12 Replies)
Discussion started by: jedlund21
12 Replies

2. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

3. Shell Programming and Scripting

Using bash to separate files files based on parts of a filename

Hey guys, Sorry for the basic question but I have a lot of files that I want to separate into groups based on filenames which I can then cat together. Eg I have: (a_b_c.txt) WB34_2_SLA8.txt WB34_1_SLA8.txt WB34_1_DB10.txt WB34_2_DB10.txt WB34_1_SLA8.txt WB34_2_SLA8.txt 77_1_SLA8.txt... (1 Reply)
Discussion started by: Breentax
1 Replies

4. Shell Programming and Scripting

Compare large file and identify difference in separate file

I have a very large system generated file containing around 500K rows size 100MB like following HOME|ALICE STREET|3||NEW LISTING HOME|NEWPORT STREET|1||NEW LISTING HOME|KING STREET|5||NEW LISTING HOME|WINSOME AVENUE|4||MODIFICATION CAR|TOYOTA|4||NEW LISTING CAR|FORD|4||NEW... (9 Replies)
Discussion started by: jubaier
9 Replies

5. Shell Programming and Scripting

Need to combine counts from diff files

Ok i have a script that goes into the last 3 files, grabs a value from that file which is a date, counts the number of results of that date. So this is the output: FILED001.20110407.175709.086912 49995 04-07T12 4 04-07T07 1 04-07T17 FILED001.20110407.180111.086913 50000 04-07T12 ... (5 Replies)
Discussion started by: cinderella
5 Replies

6. Shell Programming and Scripting

Script to compare table counts...

Hello all, I am very new to unix. I am working on a DB upgrade. The requirement is to get count of all the tables in the database before and after upgrade and compare them. I found the below query to find the counts of all the tables: nawk -v v="'" '{ print("select " v ":" $1":" v... (2 Replies)
Discussion started by: vkrisaak
2 Replies

7. Shell Programming and Scripting

Sending e-mail of record counts in 3 or more files

I am trying to load data into 3 tables simultaneously (which is working fine). Then when loaded, it should count the total number of records in all the 3 input files and send an e-mail to the user. The script is working fine, as far as loading all the 3 input files into the database tables, but... (3 Replies)
Discussion started by: msrahman
3 Replies

8. Shell Programming and Scripting

Get counts for multiple files

How do get the counts by excluding header and tailer. wc -l customer_data*.0826 31 customer_data_1.0826 57 customer_data_2.0826 456 customer_data_3.0826 668 customer_data_4.0826 789 customer_data_5.0826 2344 customer_data_6.0826 13457 customer_data_7.0826... (6 Replies)
Discussion started by: zooby
6 Replies

9. Shell Programming and Scripting

Comparing Counts Within Separate Files

Hey all, So I have this challenge where I am attempting to compare record counts from within several different log files. I want input and output counts for each file, and I want to compare that with the result of the input/output comparison from a separate--but related file. Example: ... (2 Replies)
Discussion started by: gator76
2 Replies

10. Shell Programming and Scripting

compare two .dat files and if there is any difference pulled into a separate file

Hi, compare two .dat files and difference will be moved into separate file.if anybody having code for this please send asap. using diff command, i don't know how to write shell programming. and my first file is like this including Header and trailer 10Ç20060323Ç01(Header) 01ÇIÇbabuÇ3000 01ÇIÇbaluÇ4000... (1 Reply)
Discussion started by: kirankumar
1 Replies
Login or Register to Ask a Question