Unix File Comparision


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix File Comparision
# 1  
Old 08-28-2006
Unix File Comparision

I have a file named file1 which contains numbers in sequence like...
1
2
3
7
8
Then i have file File 2 that contains
4
5
........so i need to compare both files so that i can find out the missing entry in the sequence is 6.......These files are flat files which contain and so i need to write a script that takes as input these 2 files.
I guess i cant use diff command or comm command for this , if i can how can i do it plz let me know guys.
# 2  
Old 08-28-2006
I get this result:
Code:
kcsdev:/home/jmcnama> cat file1 
1
2
3
4
5
7
8

kcsdev:/home/jmcnama> cat file2
9
10

kcsdev:/home/jmcnama> s.sh
 6 is missing

from this code:
Code:
#!/bin/ksh
# script = s.sh
let expected=1
for i in $( cat file1 file2 | sort -n -u)
do
    if [[ $expected != $i ]] ; then
        echo " $expected is missing"
        break
    fi
    let expected=$expected+1
done

# 3  
Old 08-28-2006
hey thanks for the reply......but i dont understand how u can get the missing nos from the sequence in both the files.............if i have 2 files like file1 containing
1
2
5
and then file2...
3
then i need to find out that 4 is the missing no in the sequence from both the files.........
# 4  
Old 08-28-2006
Code:
#!/bin/ksh
# script = s.sh
let expected=1
for i in $( cat file1 file2 | sort -n -u)
do
    if [[ $expected != $i ]] ; then
        echo " $expected is missing"
        expected=$i       
    fi
    let expected=$expected+1
done

This reports multiple missing elements. The example you gave above only was missing "6". If you need to find which file is missing what nubmer, use grep -q on the original files to locate the file that is missing the number.
# 5  
Old 08-28-2006
an awk version

Deleting the post

Last edited by ranj@chn; 08-28-2006 at 05:53 PM.. Reason: Post was wrong
# 6  
Old 08-29-2006
Code:
$ cat 11
1
2 
3
7
8

$ cat 22
4
5
11

$ cat 11 22 | sort -n -u | ./a
6
9
10
$ cat a
#!/bin/awk -f
BEGIN {
    i = 1;
}
/^[0-9]+/ {
    while (i < $1) {
        print i;
        i++;
    }
    i++;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing Select Columns from two CSV files in UNIX and create a third file based on comparision

Hi , I want to compare first 3 columns of File A and File B and create a new file File C which will have all rows from File B and will include rows that are present in File A and not in File B based on First 3 column comparison. Thanks in advance for your help. File A A,B,C,45,46... (2 Replies)
Discussion started by: ady_koolz
2 Replies

2. UNIX and Linux Applications

Unix Shell Scripting : Comparision of two files

Hi, We need to compare a text file File1.txt and config file File2.txt in a way that it checks if the content of File1.txt exists between the range mentioned in File2.cfg. The range here is the range between col1 and col2 of File2.cfg If the content of File1.txt lies between the range of... (12 Replies)
Discussion started by: CFA
12 Replies

3. Shell Programming and Scripting

Date comparision in Unix

Hi All, I would need your help to compare dates in my script. Say if I have the dates in a file I need to comapre these dates with yesterday's date and the dates which are older than yesterday needs to be displayed. Example: 03/22/2012 03/24/2012 03/20/2012 03/21/2012 03/12/2012... (1 Reply)
Discussion started by: pdreddy34
1 Replies

4. Shell Programming and Scripting

Comparision of two huge unix files - Reconcilation

Hi, I have two huge file; each one has approximately 150000 lines. I need to compare both of them and store the unmatched lines into a different file. I have searched for everything in google but did not get solution. Files are: File1 NRALBAMINDB20003726 NRALBAMINDB20003727... (16 Replies)
Discussion started by: Suman Singh
16 Replies

5. Shell Programming and Scripting

File Comparision

Hi All, I want to write a script which will compare two files and tell me if the files are different. Actually my files will be same but order of lines will be different,so diff is not working. I have written a script to do this:- while read line; do cnt=`grep -i $line... (6 Replies)
Discussion started by: prasson_ibm
6 Replies

6. Shell Programming and Scripting

File comparision

Hi All I have to files cat a.txt AAA BBB CCC DDD and cat b.txt AAA CCC EEE i want to compare these two files and o/p should have content of file a.txt which is not in file b.txt c.txt BBB DDD Please help me (3 Replies)
Discussion started by: aaysa123
3 Replies

7. Shell Programming and Scripting

File comparision

Hi All I have to files cat a.txt AAA BBB CCC DDD and cat b.txt AAA CCC EEE i want to compare these two files and o/p should have content of file a.txt which is not in file b.txt c.txt BBB DDD Please help me (1 Reply)
Discussion started by: aaysa123
1 Replies

8. Shell Programming and Scripting

File comparision

HI, I would like to know how to compare two files and replace non-matching lines with "_" . I can get non-mathing lines with grep -v -f file1 file2 i just want to knw how to display 'file2' with non-matching lines from 'file1' replaced by "_" for exmaple file1: a b c d ... (2 Replies)
Discussion started by: maddy81
2 Replies

9. Shell Programming and Scripting

file size comparision local file and remote file

Hi, I have written a script which would FTP a dump file to the FTP server and log the whole activity into a file. to confirm the success of the file copy i grep for "226 file receive OK" and then send out an email saying success. Now i want to make sure the bytes of the local file and... (4 Replies)
Discussion started by: dba.admin2008
4 Replies

10. Shell Programming and Scripting

How to perform comparision in unix

I am trying to perform a simple if/else check. if ; then mkdir /wdnet/oracletest else mkdir $CON_DIR fi I guess I don't understand the unix basics about comparisons. My scripts always executes the if, even though my CON_DIR variable is not blank. What am I doing... (5 Replies)
Discussion started by: artfuldodger
5 Replies
Login or Register to Ask a Question