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
# 1  
Old 11-28-2017
Shell Script to Compare Files and Email the differences

Hi,

I have 2 files abc.txt and bdc.txt.

I am using
Code:
$diff -y abc.txt bcd.txt  -- compared the files side by side

I would like to write a Shell Script to cmpare the files side by side and print the results( which are not matched) in a side by side format and save the results in another file.

Please help me to write a script.

Thanks,
Vasu
# 2  
Old 11-28-2017
Please post your work (what you have tried so far using CODE tags).

Thanks.
# 3  
Old 11-28-2017
By following the forum have copied the below code.
below code displaying result. But I would like to mail the differences as attachment which can be easy to understand for any user. ( I didnt add mailx command in the below given code)
Code:
#! /bin/sh
# rt.sh

if [[ $# -ne 2 ]] ; then
echo "Need just 2 files to compare"
exit 1
fi ;

# Take the diff by ignoring the blank and whitespaces.
diff -b -w ${1} ${2} > ${1}.diff
        if [[ $? -eq 0 ]] ; then
        echo "No files were added/removed"
        else
        echo "Files were added/removed"
        fi ;

# Process the diff file.
# A line might look like 
# < text-which-went-out
# > text-which-came-in

while read line
do
        if [[ ${line:0:2} == "< " ]] ; then
        echo ${line:2} is removed. 
        fi ;

        if [[ ${line:0:2} == "> " ]] ; then
        echo ${line:2} is added.  
        fi ;

done < ${1}.diff

rm ${1}.diff


Last edited by rbatte1; 11-28-2017 at 05:44 AM.. Reason: Changed ICODE tagds to CODE tags
# 4  
Old 11-28-2017
If you are looking for differences between two sorted list files, would you be better with grep -vf file1 file2 and then the reverse, grep -vf file2 file1 ?

I am guessing at your input because of the comments. It might save you a headache, or it might be completely wrong.

Do you have some sample input and expected output?



Robin
# 5  
Old 11-28-2017
Did you consider the comm utility?
# 6  
Old 11-29-2017
Here , where can we find the output? I gave the
Code:
grep -vf file1.txt  file2.txt

. then in 3 minutes(approx) its showing the $ prompt again. Please advise where can i find the comparison results.

Thanks


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-29-2017 at 06:17 AM.. Reason: Added CODE tags.
# 7  
Old 11-29-2017
No output mean that there are no difference. Any differences would be displayed to the screen, or you could choose to direct them to a file as usual.

As for the return code, a zero value of $? indicates that there are differences and a value of one indicates that the files match.

I presume that these are quite large files if they take 3 minutes to work through. I know that there can be performance issues with grep and the -f flag with files over a certain size. is this time critical?


I hope that this helps,
Robin
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