Script to compare 2 files and prints difference as output sidebyside

Hi All,

Am trying script to compare 2 files and print the difference found from old file to new file on line by line basis on side by side display.

Basically line by line comparision and files may contain blank line as well

I know we have compare/diff commands but i don't how to make them display the results side by side.

for ex : OLD_FILE content will be

netip :01.99.22.10
username :herty
component : sds mgm tth
 
link file : 
symbol : �$ %$
 

NEW FILE content will be

netip:11.88.23.11
username : mmerty
 
link file : gge
symbol : �$ %$
 

After running the script : ./scriptname OLD_FILE NEW FILE

Output should display like this :

line 1 is different :
OLD_FILE : netip :01.99.22.10      NEW FILE : netip:11.88.23.11
 
line 2 is diiferent : 
OLD_FILE : username :herty        NEW FILE : username : mmerty
 
line 3 is different :
OLD_FILE : component : sds mgm tth  NEW FILE  : 
 
line 4 is different :
OLD_FILE : link file :      NEW FILE : link file : gge

man diff :

$ diff -y file1 file2
netip :01.99.22.10                          |    netip:11.88.23.11
username :herty                             |    username : mmerty
component : sds mgm tth                     <
link file :                                 |    link file : gge

You may consider using the --suppress-common-lines option as well.

Using diff command is the best option here. But if you want an o/p as per your requirement, then here is another approach:

c=0
paste -d"@" OLD_FILE NEW_FILE | while IFS=@ read o_line n_line
do
        c=$(( c + 1 ))
        if [ "$o_line" != "$n_line" ]
        then
                echo "Line $c is different:"
                echo "OLD_FILE: $o_line NEW_FILE: $n_line\n"
        fi
done

@bipinajith: won't work. It doesn't sync back in once there's one or more lines too many in either file. In this case, the component line shifts the entire OLD_FILE one down, never to come back in sync.
diff (magically?) does...

I don't think this statement is correct. Below is contents of 2 files and paste o/p side-by-side:

OLD_FILE                 NEW_FILE             paste O/P
------------------------ -------------------  ------------------------------------
netip :01.99.22.10       netip:11.88.23.11    netip :01.99.22.10@netip:11.88.23.11
username :herty          username : mmerty    username :herty@username : mmerty
component : sds mgm tth                       component : sds mgm tth@
                         link file : gge       @link file : gge
link file :              symbol : #$ %$       link file : @symbol : #$ %$
symbol : #$ %$                                symbol : #$ %$@

BTW I have changed the field separator to @ because the file content has #

Well, that's a matter of opinion. Result of your code working on requestor's files:

Line 1 is different:
OLD_FILE: netip :01.99.22.10 NEW_FILE: netip:11.88.23.11
Line 2 is different:
OLD_FILE: username :herty NEW_FILE: username : mmerty
Line 3 is different:
OLD_FILE: component : sds mgm tth NEW_FILE:  
Line 4 is different:
OLD_FILE:   NEW_FILE: link file : gge
Line 5 is different:
OLD_FILE: link file :  NEW_FILE: symbol : �$ %$
Line 6 is different:
OLD_FILE: symbol : �$ %$ NEW_FILE: 

You are right when you say line 5 etc. is different, but old line 6 equals new line 5, so a resync should take place (as does diff ). Else long files will be entirely different should line 1 be lost... The requestor's sample output vaguely alludes he/she wants resync.

Anyhow, I regretfully apologize for the harsh wording.

You're absolutely correct about using diff command. I mentioned it already in Post #3 that using diff command is the best option here.

I was just suggesting a different approach as per requester's requirement.

you can use sdiff, this does what you want...

Thanks Bipinajith. your idea does works very well but my only concern is in output display i am getting even "/n". so am bit confussed here does it considering "/n" as well while comparing.

output looks like this :

./cmpconfig.sh OLD_FILE NEW_FILE
Line 1 is different:
OLD_FILE: netip :01.99.22.10 NEW_FILE: netip:11.88.23.11\n
Line 2 is different:
OLD_FILE: username :herty NEW_FILE: username : mmerty\n
Line 3 is different:
OLD_FILE: component : sds mgm tth NEW_FILE: \n
Line 4 is different:
OLD_FILE:  NEW_FILE: link file : gge\n
Line 5 is different:
OLD_FILE: link file :  NEW_FILE: symbol : �$ %$\n
Line 6 is different:
OLD_FILE: symbol : �$ %$ NEW_FILE: \n

Moreover, output shouldn't have displayed Line 5 and 6, since both line values are same. Even don't want to display \n at the end. Could you please tell me how to avoid this ?

Many Thanks
Optimus81

Use -e option with echo command to enable interpretation of the backslash-escaped characters.

RudiC mentioned in earlier post that diff commands syncs input lines. So I recommend using it to resolve issue with line 5 & 6.

1 Like

Thanks bipinajith and to everyone.

diff or sdiff will work as well but using sdiff -s opition, we won't get the output as required.