CMP two files with slight difference and return code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CMP two files with slight difference and return code
# 1  
Old 10-08-2008
CMP two files with slight difference and return code

I am comparing two files which are identical except for the timestamp which is incorporated within the otherwise same 372 bytes. I am using the command:

cmp -s $Todays_file $Yesterdays_file -i 372

When I run the command without the -i 372 it shows the difference i.e. the timestamp. However, with -i 372 it works as expected displays no differences as it ignores the first 372 bytes.

However, in both cases the return code is set to 1. I was hoping that in the second case the return code would be set to 0 i.e. no changes as we are ignoring the first 372 bytes.

I do some further processing based on the return code. Is there a way I can compare the two files?
# 2  
Old 10-08-2008
My "POSIX" compliant cmp doesn't contain this "-i" options. It sounds cool, but it doesn't have it. I also don't know what cmp would print out the timestamp difference.

So how about running each file through a process that strips the first 372 bytes, then compare the resulting files?
Code:
dd if=file1 bs=1 skip=372 of=file1.372 >/dev/null
dd if=file2 bs=1 skip=372 of=file2.372 >/dev/null
cmp -l file[12].372
rm -f file[12].372

# 3  
Old 10-08-2008
Hi.

The GNU cmp seems to work for me:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate cmp, skipping initial bytes.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) cmp
set -o nounset
echo

FILE1=data1
FILE2=data2

echo " Data file $FILE1:"
cat $FILE1

echo
echo " Data file $FILE2:"
cat $FILE2

echo
echo " Results for comparison of entire files:"
cmp $FILE1 $FILE2
echo " Exit code is $?"

echo
echo " Results for comparison of files past byte 5:"
cmp -i 5 $FILE1 $FILE2
echo " Exit code is $?"

echo
echo " Results for comparison options in non-standard order:"
cmp $FILE1 $FILE2 -i 5
echo " Exit code is $?"

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
cmp (GNU diffutils) 2.8.1

 Data file data1:
junk1 in this line.
hello, world.

 Data file data2:
junk2 in this line.
hello, world.

 Results for comparison of entire files:
data1 data2 differ: char 5, line 1
 Exit code is 1

 Results for comparison of files past byte 5:
 Exit code is 0

 Results for comparison options in non-standard order:
 Exit code is 0

Are you taking into account that the exit code is reset for each command? ... cheers, drl
# 4  
Old 10-08-2008
It's not documented in the man page. Not even in the info pages. I've got the same version.
# 5  
Old 10-08-2008
Hi, otheus.
Quote:
Originally Posted by otheus
It's not documented in the man page. Not even in the info pages. I've got the same version.
Interesting. Are you using Linux? I have worked on unix systems where the SA installed the GNU utilities, but did not install man pages. Sometimes the GNU utilities were in odd places, and the PATHs were adjusted.

Does the utility accept the the "-i" option? ... cheers, drl
# 6  
Old 10-09-2008
Yeah, cmp works just fine with the -i option. cmp --help shows it too. "rpm" shows that the package was installed in 2002 and that the man pages came along with it. Sounds like the man pages were never updated.
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 and capture return code

Hi, I would like to compare 2 files, and have a return code write to a file. regardless of the files contents are the same the code should be writing to a file (if both files contents are same then return code 0). A simple example will be great :) Thanks (3 Replies)
Discussion started by: khchong
3 Replies

2. Shell Programming and Scripting

How to compare files in two folders using cmp?

i recently copied 400GB of data from a NTFS drive to a ext4 drive. I want to verify that the data is 100% identical to the original. I wanted to use cmp but it only does two files. The directory that was copied contains many subdirectories and all sorts of files (not just text). So I guess... (5 Replies)
Discussion started by: fuzzylogic25
5 Replies

3. Solaris

cmp file

Dear all, for i in <List of Filename> FILENAME=`echo $i` do cp -p $FILENAME /temp /bin/cmp $FILENAME /temp/$FILENAME done I am planning to do something like this on a daily basis, so i want to ask that, if the comparison on the files encounter error, ... (2 Replies)
Discussion started by: beginningDBA
2 Replies

4. Shell Programming and Scripting

How to merge two files with a slight twist

Hi, a brief introduction on the soundex python module(english sound comparison): import soundex.py a = "neu yorkk" b = "new york city" print soundex.sound_similar(a, b) output: 1 Suppose I want to merge two files, called mergeleft.csv and mergeright.csv Mergeleft.csv: ... (0 Replies)
Discussion started by: grossgermany
0 Replies

5. Shell Programming and Scripting

Issue in comparing (cmp) two files

Hi All, I have to compare set of files so I created a case statement with the option to give more than one file to compare. The Problem now i am facing is, if I compare the files directly, from prompt or just using the script only for a particular file then It's saying No difference, but If I... (4 Replies)
Discussion started by: Sudhar
4 Replies

6. Shell Programming and Scripting

date cmp

Please don't count this as a similar post.....I got the ftp part working....I am stuck how to find the files between two dates. I have 5 files filename.20090505.txt filename.20090504.txt filename.20090503.txt filename.20090502.txt filename.20090501.txt My load date is 20090501 and run date... (5 Replies)
Discussion started by: RubinPat
5 Replies

7. UNIX for Dummies Questions & Answers

cmp 2 variables

Hi I have two variables contining a set of near identical lines, i'd like to list the lines that differ? Prefereably i'd like not to save the variables into a file first. i.e var1 tag:val1 tag:val2 tag:val3 var2 tag:val1 tag:val4 tag:val3 i'd like the result to print out... (2 Replies)
Discussion started by: nickrick
2 Replies

8. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

9. Shell Programming and Scripting

cmp, diff need options

Hi, I am using diff filename1 filename2, as these files are of huge size,I want to know the count(n) no. of different records to be displayed on the terminal. I do not want the contents of file i mean different lines to be displayed. Cheers Kunal. (0 Replies)
Discussion started by: niceboykunal123
0 Replies

10. UNIX for Dummies Questions & Answers

Having a slight problem!?

having a slight problem. any clues would help. Can't seem to get any output when I run a simple echo script. grex.cyberspace.org% chmod a+x test grex.cyberspace.org% ls -l test -rwxrwx--x 1 gordybh cohorts 20 Dec 13 20:22 test grex.cyberspace.org% cat test #!/bin/sh echo test... (2 Replies)
Discussion started by: wmosley2
2 Replies
Login or Register to Ask a Question