Help comparing 2 files and sending differences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help comparing 2 files and sending differences
# 1  
Old 01-09-2013
Help comparing 2 files and sending differences

I have 2 files that need to be compared. Email the differences if something is different and don't email if nothing is different. One or both of the files could be empty. One or both could have data in them.

example files
backup.doc.$(date +%y%m%d) file size is 0
backup.doc.$(TZ=CST+24 date +%y%m%d) file size is 412

or vice versa

Any help would be appreciated. I've tried the following but it seems to ignore if there is nothing in the file.

Code:
awk 'NR==FNR{a[$0]++;next} !($0 in a) {print $0}' backup.doc.$(date +%y%m%d)  backup.doc.$(TZ=CST+24 date +%y%m%d) > /tmp/failed
[ -s /tmp/failed ]  && cat /tmp/failed | mailx -s "Backup Failed" user@email.com

# 2  
Old 01-09-2013
Quote:
Originally Posted by jabbott3
I have 2 files that need to be compared.
Do it with "diff". See "man diff" for details.

Quote:
Originally Posted by jabbott3
Email the differences if something is different and don't email if nothing is different.
Check the output of "diff" - if the files are identical the output will be empty and the return code of "diff" will also reflect that. You can use this (as well as any eventual output) to construct your mail the same way you already did in your scripts version.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 01-09-2013
thank you!

Sorry, one more question, can I make it not email if file2 is 0 even though there are still differences?

Last edited by bakunin; 01-09-2013 at 11:07 AM..
# 4  
Old 01-09-2013
Quote:
Originally Posted by jabbott3
Sorry, one more question, can I make it not email if file2 is 0 even though there are still differences?
Of course you can:

Code:
if [ $(ls -s file2 | cut -f1) -gt 0 ] ; then
     diff file1 file2 > tmpfile
     if ....
          mail -s .....
     fi
fi

"ls -s" is the file size in blocks, if the file is empty it is 0.

I hope this helps.

bakunin
# 5  
Old 01-09-2013
Thank you so very much!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing time differences between 2 Solaris servers

Good day to all. I'm relatively new in using the Sun Solaris OS. I would like to request your expertise in helping to solve a problem that I have at work. Not sure if this has been asked before but I have tried searching through the internet to no avail. Basically I have 2 sun solaris... (8 Replies)
Discussion started by: Fossil_84
8 Replies

2. Shell Programming and Scripting

Comparing 2 xml files and print the differences only in output

Hi....I'm having 2 xml files, one is having some special characters and another is a clean xml file does not have any special characters. Now I need one audit kind of file which will show me only from which line the special characters have been removed and the special characters. Can you please... (1 Reply)
Discussion started by: Krishanu Saha
1 Replies

3. Shell Programming and Scripting

Comparing 2 CSV files and sending the difference to a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below: file1.csv ID,version,cost 1000,1,30 2000,2,40 3000,3,50 4000,4,60 file2.csv ID,version,cost 1000,1,30 2000,2,45 3000,4,55 6000,5,70 ... (1 Reply)
Discussion started by: Naresh101
1 Replies

4. Shell Programming and Scripting

Comparing two files and list the differences

Hi * I have two text files which has the file size, timestamp and the file name. I need to compare these two files and get the differences in the output format. Can anyone help me out with this. * cat file1.txt *474742 Apr 18* 2010 sample.log *135098 Apr 18* 2010 Testfile 134282 Apr 18* 2010... (7 Replies)
Discussion started by: Sendhil.Kumaran
7 Replies

5. Shell Programming and Scripting

Perl: Comparing to two files and displaying the differences

Hi, I'm new to perl and i have to write a perl script that will compare to log/txt files and display the differences. Unfortunately I'm not allowed to use any complied binaries or applications like diff or comm. So far i've across a code like this: use strict; use warnings; my $list1;... (2 Replies)
Discussion started by: dont_be_hasty
2 Replies

6. Shell Programming and Scripting

Differences between 2 Flat Files and process the differences

Hi Hope you are having a great weeknd !! I had a question and need your expertise for this : I have 2 files File1 & File2(of same structure) which I need to compare on some columns. I need to find the values which are there in File2 but not in File 1 and put the Differences in another file... (5 Replies)
Discussion started by: newbie_8398
5 Replies

7. Shell Programming and Scripting

Comparing files columnwise and print the differences in third file

Hello Everybody!!!!!!!!! Request you to help me with the below mentioned issue: I have 2 files say, File 1: a|4|7 b|3|2 c|8|8 d|8|9 File 2: a|4|6 b|2|2 c|8|8 d|9|8 The third file(output file) should have: Data mismatch in row 1 column 3 Data mismatch in row 2 coumn 2 Data... (3 Replies)
Discussion started by: abhijeet1409
3 Replies

8. Shell Programming and Scripting

Comparing rows in two tables and sending the differnce to mail

Hi, I have a table ,containg 2 coloumns and many rows,which is updated everyday.The no.of rows in the table changes everyday. i have to write the difference between yesterdays tabtle and todays table to a new file.The row that is new in the todays table need not to be shown.only the change... (2 Replies)
Discussion started by: bab123
2 Replies

9. Shell Programming and Scripting

comparing file content differences

I need to write a script to find out if there are any .c files created/removed from the last time i monitored the files available. i first created a file to contain all the .c files available on the system. (ls *.c > file1) I created another file using the same command. I used the comm file1... (4 Replies)
Discussion started by: RianTan
4 Replies

10. UNIX for Dummies Questions & Answers

Number of differences between 2 files

Hi, "diff" command takes two file names as arguements and gives the difference between the two. How do I get the number of differences between two files ??? (Excluding whitespaces). Don't ask me to count number of lines produced by "diff". Thanks in advance, Sharath (4 Replies)
Discussion started by: sharuvman
4 Replies
Login or Register to Ask a Question