Shell Script to Compare Files and Email the differences

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Shell Script to Compare Files and Email the differences
# 8  
Old 11-29-2017
To avoid having a filename in one file like file1 match longer names in the other file like file10, file101, etc. and to speed things up a little bit, you might want to try:
Code:
grep -vFxf file1.txt file2.txt

instead of:
Code:
grep -vf file1.txt file2.txt

The -F option tells grep to match fixed strings instead of basic regular expressions and the -x option tells grep to only look for full-line matches.

On some older systems, you might need to use fgrep instead of grep -F:
Code:
fgrep -vxf file1.txt file2.txt

These 2 Users Gave Thanks to Don Cragun For This Post:
# 9  
Old 11-29-2017
There is difference in the files as the size of the files as are below.
Code:
FILE1.txt   156665

FILES2.txt 154359

But its not displaying the differences. Please advise how to get the differences and mail it as attachment.

Thanks

---------- Post updated at 06:22 PM ---------- Previous update was at 06:15 PM ----------

Don,

Now am able to see the differences in a file. Can you advise How can I export these differences (which will be Side by side) into local desktop to understand and compare easily.

As I can see the differences by using
diff -y file1.txt files2,txt




Thanks

Last edited by rbatte1; 11-29-2017 at 08:47 AM.. Reason: Changed ICODE tags to CODE tags
# 10  
Old 11-29-2017
You may want to add - given your diff version provides it - the --suppress-common-lines option.
# 11  
Old 11-29-2017
Quote:
Originally Posted by vasuvv
There is difference in the files as the size of the files as are below.
Code:
FILE1.txt   156665

FILES2.txt 154359

But its not displaying the differences. Please advise how to get the differences and mail it as attachment.

Thanks

---------- Post updated at 06:22 PM ---------- Previous update was at 06:15 PM ----------

Don,

Now am able to see the differences in a file. Can you advise How can I export these differences (which will be Side by side) into local desktop to understand and compare easily.

As I can see the differences by using
diff -y file1.txt files2,txt




Thanks
There are at least two issues here.

First, knowing that the files FILE1.txt and FILES2.txt have a different size tells us those two files are different, but it doesn't tell us whether or not any line in either file is unique. The command diff -y f1 f2 will point out line-by-line any similarities and any differences between those two files. The command grep -vFxf f1 f2 will tell you if there are any lines in f2 that do not also appear as a line in f1. For example, if f1 contains the single line:
Code:
a

and f2 contains the three lines:
Code:
a
a
a

that diff will tell you:
Code:
a								a
							      >	a
							      >	a

while that grep command will find no differences (because every line in f2 is also a line somewhere in f1). Similarly, if f1 contains:
Code:
a
b
c

and f2 contains:
Code:
c
b
a

the diff command will show you that at most one line is the same between the two files while the grep will not find any differences. If your two files contains lists of filenames (especially if those files are not sorted), the grep commands will give you useful information. The diff -y command will give you a lot of information, but it might not be useful.

Second, knowing that files FILE1.txt and FILES2.txt are different says absolutely nothing about whether or not file1.txt and files2,txt are different. UNIX, Linux, and BSD filesystems are case sensitive. And, even on filesystems that are not case-sensitive, a comma and a period are different.

Until you tell us what operating system you're using AND show us what you have tried with mailx there is little that we can do to help you with adding attachments to mail. The mailx utility in the standards doesn't have an option to add attachments. The mailx utility on many systems does have an option to add an attachment to a mail message, but that option can vary from system to system or not be provided at all.

We may also need to know what mail reader will be used by the person receiving your email. Some mail readers recognize a wide variety of attachment types; others do not.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Linux/Shell script - How to compare 2 arrays based on patterns and get the differences

I have FILE 1 (This file has all master columns/headers) A|B|C|D|E|F|G|H|STATUS FILE 2 A|C|F|I|OFF_STATUS 3|4|5|4|Y 6|7|8|5|Y Below command give me all headers of FILE 2 into array2.txt file paste <(head -1 FILE2.txt | tr '|' '\n')>array2.txt So I would like to compare... (2 Replies)
Discussion started by: jmadhams
2 Replies

2. Shell Programming and Scripting

Compare directories and copy differences (files) in a another directory

Hey im working on script that can compare 2 directory and check difference, then copy difference files in third diretory. here is the story: in folder one we have 12 subfolder and in each of them near 500 images hosted. 01 02 03 04 05 06 07 08 09 10 11 12 in folder 2 we have same subfolder... (2 Replies)
Discussion started by: nimafire
2 Replies

3. UNIX for Beginners Questions & Answers

Compare two big files for differences using Linux

Hello everybody Looking for help in comparing two files in Linux(files are big 800MB each). Example:- File1 has below data $ cat file1 5,6,3 2.1.4 1,1,1 8,9,1 File2 has below data $ cat file2 5,6,3 8,9,8 1,2,1 2,1,4 (8 Replies)
Discussion started by: shanul karim
8 Replies

4. Shell Programming and Scripting

Compare two big files for differences using Linux

Hello everybody Looking for help in comparing two files in Linux(files are big 800MB each). Example:- File1 has below data $ cat file1 5,6,3 2.1.4 1,1,1 8,9,1 File2 has below data $ cat file2 5,6,3 8,9,8 1,2,1 2,1,4 (1 Reply)
Discussion started by: shanul karim
1 Replies

5. Shell Programming and Scripting

Need to compare the two files and list out differences between the two

Hi, I need to compare the two files and list out difference between the two. Please assist. Best regards, Vishal (2 Replies)
Discussion started by: Vishal_dba
2 Replies

6. Shell Programming and Scripting

Compare two files using shell script

Hi i want to compare two files and i need the o/p of only difference here the files file1 achilles aedxbepo aedxbwdm01 aedxbwdm02 albedo amarice ambrister anakin anton argon artephius asgard avatar aymara (10 Replies)
Discussion started by: venikathir
10 Replies

7. Shell Programming and Scripting

Shell script to compare two files

I have two files; file A and file B. I need all the entries of file A to be compared with file B line by line. If the entry exists on file B, then save those on file C; if no then save it on file D Note :- all the columns of the lines of file A need to be compared, except the last two columns... (8 Replies)
Discussion started by: ajiwww
8 Replies

8. Shell Programming and Scripting

Compare two text files and Only show the differences

Hi experts, I'mvery new to shell scripting and learning it now currently i am having a problem which may look easy to u :) i have two files File 1: Start :Thu Nov 19 10:33:09 2009 ABCDGFSDJ.txt APDemoNew.ppt APDemoOutline.doc ARDemoNew.ppt ARDemoOutline.doc File 2: Start... (10 Replies)
Discussion started by: CelvinSaran
10 Replies

9. UNIX and Linux Applications

How to compare two files using shell script

hi experts please help me to compare two files which are in different directory file1<file will be master file> (/home/rev/mas.txt} ex x1 x2 file2 <will be in different folder> (/home/rev/per/.....) ex x3 x4 the filesinside per folder i need to compare with master file... (1 Reply)
Discussion started by: revenna
1 Replies

10. UNIX for Dummies Questions & Answers

Compare 2 files for a single column and output differences

Hi, I have a column in 2 different files which i want to compare, and output the results to a different file. The columns are in different positions in those 2 files. File 1 the column is in position 10-15 File 2 the column is in position 15-20 Please advise Thanks (1 Reply)
Discussion started by: samit_9999
1 Replies
Login or Register to Ask a Question