Comparing the values of two files

Hi
Am trying to compare the values of two files..
One is a big file that has many values and the other is a small file..
The big file has all values present in small file..

# cat SmallFile
4456602 22347881
7471282 15859891
8257690 21954701
7078068 18219229
2883826 6094959
100000

#cat BigFile
2556026 4456607
1638400 26935353
1638400 27000891
1638400 27066427
1638400 27131965
2556026 4522129
2556026 4587665
2556026 4653243
2556026 4718739
2556026 4784279
2162828 4849867
2556026 4915351
2556026 4980889
9502866 51904709
2556026 5046427
6553624 52232395
2556026 5111965
2556026 5177503
2556026 5243041
7536864 52560077
8519918 52625643
9568272 52887787
3080288 52953273
13828302 53084411
11337738 53149809
4456602 22347881
7471282 15859891
8257690 21954701
7078068 18219229
2883826 6094959

My problem is when i tried to compare the value present is SmallFile that is not present in BigFile.. My code should return error
But it is returning the return value of grep command as zero

#!/usr/bin/ksh
#set -x
count=`cat SmallFile | wc -l`

while [[ $count -gt 0 ]] && read LINE ; do
    d=$LINE
    i=`echo $d | cut -f1 -d " "`
    td=`echo $d | cut -f2 -d " "`

    fmp=`cat BigFile | grep "$i" | awk -F " " '{print $2}'`
    RetVal=`echo $?`
    echo "The RC val is $RetVal and i value is $i"

    if [[ $RetVal -eq 0 ]]; then
        if [[ $td -eq $fmp ]]; then
            echo "The values from Small File is $td and from BigFile is $fmp"
        fi
    else
        echo "Cannot find match for $i"
    fi

    count=$(($count-1))
done < SmallFile

Here the "RetVal" when $i=100000 is one.. But it is returning zero..
Please help..:confused:

I noticed that you used a lot of external utilities like cat, grep, awk, cut when most of these tasks can be done by shell built-ins.

Always use shell built-ins where ever possible, it will help significantly improve the performance of your script.

I'm not sure if I understood your requirement correctly, but see few corrections and suggestions below:

#!/bin/ksh

while read s_id_1 s_id_2
do
        FLAG=0
        while read b_id_1 b_id_2
        do
                if [ $s_id_1 -eq $b_id_1 ]
                then
                        print "The value in small file is $s_id_2 and in big file is $b_id_2"
                        FLAG=1
                fi
        done < BigFile

        [ $FLAG -eq 0 ] && print "Cannot find a match for $s_id_1"

done < SmallFile
1 Like

Hi I tried the following code but i get error in the if statement

#!/bin/ksh
#set -x
while read s_id_1 s_id_2
do
        print "The value of small file is $s_id_1 and $s_id_2"
        FLAG=0
        while read b_id_1 b_id_2
        do
                echo  "The value of bigfile is $b_id_1 and  $b_id_2"
                if [ $s_id_1 -eq $b_id_1 ]
                then
                        echo "The value in small file is $s_id_2 and in big file is $b_id_2"
                        FLAG=1
                fi
        done < BigFile

        [ $FLAG -eq 0 ] && echo "Cannot find a match for $s_id_1"

done < SmallFile

The output error is

./new.sh[9]: pid: 0403-009 The specified number is not valid for this command.

Please help..

Check and verify if you have any control characters in your file:

od -c filename

Also set xtrace / verbose and debug your script to understand what is causing this error:

#!/bin/ksh -xv