Comparing the values of two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing the values of two files
# 1  
Old 05-16-2013
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..
Code:
# 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

Code:
#!/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..Smilie
# 2  
Old 05-16-2013
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:
Code:
#!/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

This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-17-2013
Hi I tried the following code but i get error in the if statement
Code:
#!/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
Code:
./new.sh[9]: pid: 0403-009 The specified number is not valid for this command.

Please help..
# 4  
Old 05-17-2013
Check and verify if you have any control characters in your file:
Code:
od -c filename

Also set xtrace / verbose and debug your script to understand what is causing this error:
Code:
#!/bin/ksh -xv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting and comparing values

I was trying to extract value of g1 and p1 only inside the tags where t1 is "Reading C (bytes)" and comparing them to make sure p1 is always less than g1. Here is the Json file I'm using - File:- { "g1" : 1482568, "n1" : "v_4", "p1" : 0, "s1" : "RC", "t1" : "LM", } { "g1" :... (3 Replies)
Discussion started by: Mannu2525
3 Replies

2. Shell Programming and Scripting

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

3. UNIX for Dummies Questions & Answers

Comparing two test files and printing out the values that do not match

Hi, I have two text files with matching first columns. Some of the values in the second column do not match. I want to write a script to print out the rows (only the first column) where the values in the second column do not match. Example: Input 1 A 1 B 2 C 3 D 4 Input 2 A 2 B 2... (6 Replies)
Discussion started by: evelibertine
6 Replies

4. UNIX for Dummies Questions & Answers

Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that? File 1: 100 rs3794811 0.01 0.3434 100 rs8066551 0.01... (8 Replies)
Discussion started by: evelibertine
8 Replies

5. UNIX for Dummies Questions & Answers

Comparing two files and dividing the values

Hi all, I am new to unix and I am trying hard to get this requirement, but no luck. I am trying to compare two cloumns in two files and if it matches, the last column in file1 must be divided by file2 and the output must be written in a new file. To elaborate the 2nd column in file1 (EUR) must be... (6 Replies)
Discussion started by: smadderla
6 Replies

6. Shell Programming and Scripting

Comparing values in column 1

Hi to all, I have the following text within inputfile data1,value1,value2 data1,value3,value2 data1,value5,value6 data2,value1,value2 data2,value3,value4 data3,value1,value2 data3,value3,value4 data4,value1,value2 data4,value3,value4 data4,value5,value6 I would like to... (4 Replies)
Discussion started by: cgkmal
4 Replies

7. Shell Programming and Scripting

comparing values

i have two file and i am comparing both.. in cmp1 ,the content is : the nu of file is : <some integer value> in cmp2 ,the content is : the nu of file is : so want a script which will take value (2) when cmp1 is compared with cmp2.. i mean cmp cmp1 cmp2 the the output will be he nu of... (1 Reply)
Discussion started by: Aditya.Gurgaon
1 Replies

8. Shell Programming and Scripting

comparing 2 text files to get unique values??

Hi all, I have got a problem while comparing 2 text files and the result should contains the unique values(Non repeatable). For eg: file1.txt 1 2 3 4 file2.txt 2 3 So after comaping the above 2 files I should get only 1 and 4 as the output. Pls help me out. (7 Replies)
Discussion started by: smarty86
7 Replies

9. Shell Programming and Scripting

comparing values of same variable

Hello all while writing a small script , i got stuck with this simple thing.Hope you guyz can help. Iam trying to read password echo"enter password" read $pwd now i have to check this echo"enter password to proceed" read $pwd now i have to check both the values of the... (2 Replies)
Discussion started by: coolkid
2 Replies

10. Shell Programming and Scripting

comparing PID values of 2 Files in shell Options

Hi, There is a file having a list of running PIDs and another file having a list of registered PIDs. How can we check if each value of running PIDs are less or more than the registered PIDs i.e. comparing the value of each PID in both the files. Request you to pls give your inputs. ... (2 Replies)
Discussion started by: marconi
2 Replies
Login or Register to Ask a Question