Script to compare 2 files and prints difference as output sidebyside


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to compare 2 files and prints difference as output sidebyside
# 1  
Old 01-12-2013
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
Code:
netip :01.99.22.10
username :herty
component : sds mgm tth
 
link file : 
symbol : £$ %$

NEW FILE content will be
Code:
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 :
Code:
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

# 2  
Old 01-12-2013
man diff:
Quote:
-y, --side-by-side
output in two columns
Code:
$ 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.
# 3  
Old 01-12-2013
Using diff command is the best option here. But if you want an o/p as per your requirement, then here is another approach:
Code:
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


Last edited by Yoda; 01-12-2013 at 04:11 PM.. Reason: Changed the field separator
# 4  
Old 01-12-2013
@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...
# 5  
Old 01-12-2013
Quote:
Originally Posted by RudiC
@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.
I don't think this statement is correct. Below is contents of 2 files and paste o/p side-by-side:
Code:
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 #
# 6  
Old 01-12-2013
Well, that's a matter of opinion. Result of your code working on requestor's files:
Code:
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.

Last edited by RudiC; 01-12-2013 at 04:32 PM..
# 7  
Old 01-12-2013
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script which telnets to a device, runs commands and prints output to a file

I am connecting to a device using telnet, I want my script to perform certain commands : ie- show device , show inventory..etc and write the output it sees from the terminal to a file. this is what I have got : #!/usr/bin/expect -- set running 1 spawn telnet <ip address> expect ... (1 Reply)
Discussion started by: samantha123
1 Replies

2. Shell Programming and Scripting

Compare two text files and output difference

Hi experts, I am trying to compare two text files and output the difference to another file. I'm not strictly looking for differences in text but additional text at the end of one file that isn't in another, so basically comparing the file 2 against file 1 and printing any additional text to... (9 Replies)
Discussion started by: martin0852
9 Replies

3. Shell Programming and Scripting

compare multiple files and get the difference

Hi all, i have 50 files .data should be same in these 50 files , so my task is to find the difference. i need a logic , which finds difference between all files and print in output file with file name where it found that difference . i tried below logic , but its not giving me what i want. let... (2 Replies)
Discussion started by: deepakiniimt
2 Replies

4. Shell Programming and Scripting

Compare two files and output difference, by first field using awk.

It seems like a common task, but I haven't been able to find the solution. vitallog.txt 1310,John,Hancock 13211,Steven,Mills 122,Jane,Doe 138,Thoms,Doe 1500,Micheal,May vitalinfo.txt 12122,Jane,Thomas 122,Janes,Does 123,Paul,Kite **OUTPUT** vitalfiltered.txt 12122,Jane,Thomas... (2 Replies)
Discussion started by: charles33
2 Replies

5. UNIX for Advanced & Expert Users

Shell Script to compare xml files and print output to a file

All, PLease can you help me with a shell script which can compare two xml files and print the difference to a output file. I have attached one such file for you reference. <Group> <Member ID=":Year_Quad:41501" childCount="4" fullPath="PEPSICO Year-Quad-Wk : FOLDER.52 Weeks Ending Dec... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

6. UNIX for Dummies Questions & Answers

compare / difference between sub-sections of files

Hi there, I'm sure this question has been asked many times but I can't find any posts with information. How can I check the differences between say lines 20 - 200 in file1 and lines 420 - 600 in file2? Thanks in advance for any help! js (2 Replies)
Discussion started by: js8765
2 Replies

7. Shell Programming and Scripting

Compare two columns in two files and print the difference

one file . . importing table employee 119 . . importing table jobs 1 2nd file . . importing table employee 120 . . importing table jobs 1 and would like... (2 Replies)
Discussion started by: jhonnyrip
2 Replies

8. UNIX for Dummies Questions & Answers

Compare Files and Output Difference

I have to compare two files for any differences, then output the lab and question number for any differences. This is what I currently have: diff lab2.txt lab2answer.txt > lab2compare.txt Though the output doesn't have to be sent to a .txt (or any sort of log), I found that easier, at least... (2 Replies)
Discussion started by: Joesgrrrl
2 Replies

9. Shell Programming and Scripting

script to compare first column of two files and find difference

Hi, I want to write a script which will compare the 1st column of both the files and will give the difference. e.g:- my 1st file contains: 89 /usr 52 /usr/local 36 /tmp 92 /opt 96 /home 27 /etc/opt/EMCom 1 ... (3 Replies)
Discussion started by: adityam
3 Replies

10. Shell Programming and Scripting

to compare two files and to print the difference

suppose one file P1168S P2150L P85L Q597R R1097C Another file P2150L P85L Q597R R1097C R1379C R1587K Then output shud be R1379C R1587K thanks (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question