Using Diff to Compare 2 files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Using Diff to Compare 2 files

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-07-2010
ladyAnne ladyAnne is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 9 September 2011, 1:25 AM EDT
Posts: 83
Thanks: 10
Thanked 0 Times in 0 Posts
Using Diff to Compare 2 files

Hi

I've been trying various methods that I have found online with regards to comparing 2 files using the diff command. Nothing seems to work. The problem is that I'm not too familiar with the proper syntax. Can you please assist me. Here is my script:


Code:
#!/bin/bash
awk -F',' -v file1="$1" -v file2="$2" '{print;}1' OFS="," diff $1 $2 > resultfile.csv

So I'm parsing the two files from the command line, performing a diff on them, and then piping the results to another file.

Here is my command line:


Code:
./csv_file_compare.sh newfile1.csv newfile2.csv

file1:


Code:
User Parameter,Entity Name,Cell ID,Type SubCell / TX / FHSY / DRI / TDMA,Instance CHGR / TX / FHSY / DRI / TDMA,CellR ID,Vendor Parameter,Planned Value,Translated Value,Network Value,Override Level,Override Node,,,,Vendor,Technology,Version
User Parameter,Entity Name,Cell ID,Type SubCell / TX / FHSY / DRI / TDMA,Instance CHGR / TX / FHSY / DRI / TDMA,CellR ID,Vendor Parameter,Planned Value,Translated Value,Network Value,Override Level,Override Node,,,,Vendor,Technology,Version
,,,,,,,,,,,,,,
,,,,,,,,,,,,,,
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,5,5,1,CELL,1A,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,5,5,1,CELL,1A,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1B,,,,AGBLK,2,2,1,CELL,1B,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1B,,,,AGBLK,2,2,1,CELL,1B,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1C,,,,AGBLK,3,3,1,CELL,1C,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1C,,,,AGBLK,3,3,1,CELL,1C,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,2A,,,,AGBLK,7,7,1,CELL,2A,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,2A,,,,AGBLK,7,7,1,CELL,2A,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,2B,,,,AGBLK,4,4,1,CELL,2B,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,2B,,,,AGBLK,4,4,1,CELL,2B,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,2C,,,,AGBLK,0,0,1,CELL,2C,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,2C,,,,AGBLK,0,0,1,CELL,2C,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3A,,,,AGBLK,1,1,1,CELL,3A,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3A,,,,AGBLK,1,1,1,CELL,3A,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3B,,,,AGBLK,6,6,1,CELL,3B,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3B,,,,AGBLK,6,6,1,CELL,3B,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3C,,,,AGBLK,4,4,1,CELL,3C,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3C,,,,AGBLK,4,4,1,CELL,3C,,,,ERICSSON,GSM,08B

file2:


Code:
User Parameter,Entity Name,Cell ID,Type SubCell / TX / FHSY / DRI / TDMA,Instance CHGR / TX / FHSY / DRI / TDMA,CellR ID,Vendor Parameter,Planned Value,Translated Value,Network Value,Override Level,Override Node,,,,Vendor,Technology,Version
User Parameter,Entity Name,Cell ID,Type SubCell / TX / FHSY / DRI / TDMA,Instance CHGR / TX / FHSY / DRI / TDMA,CellR ID,Vendor Parameter,Planned Value,Translated Value,Network Value,Override Level,Override Node,,,,Vendor,Technology,Version
,,,,,,,,,,,,,,
,,,,,,,,,,,,,,
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1B,,,,AGBLK,2,2,1,CELL,1B,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1B,,,,AGBLK,2,2,1,CELL,1B,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1C,,,,AGBLK,3,3,1,CELL,1C,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1C,,,,AGBLK,3,3,1,CELL,1C,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,2A,,,,AGBLK,7,7,1,CELL,2A,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,2A,,,,AGBLK,7,7,1,CELL,2A,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,2B,,,,AGBLK,4,4,1,CELL,2B,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,2B,,,,AGBLK,4,4,1,CELL,2B,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,2C,,,,AGBLK,0,0,1,CELL,2C,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,2C,,,,AGBLK,0,0,1,CELL,2C,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3A,,,,AGBLK,1,1,1,CELL,3A,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3A,,,,AGBLK,1,1,1,CELL,3A,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3B,,,,AGBLK,6,6,1,CELL,3B,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3B,,,,AGBLK,6,6,1,CELL,3B,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3C,,,,AGBLK,4,4,1,CELL,3C,,,,ERICSSON,GSM,08B
Access Grant Blocks Reserved,CELL,3C,,,,AGBLK,4,4,1,CELL,3C,,,,ERICSSON,GSM,08B

Can anyone help me please.

Thank you

---------- Post updated at 04:18 AM ---------- Previous update was at 04:09 AM ----------

I have also tried this:


Code:
awk -F',' -v file1="$1" -v file2="$2" 'NR == FNR {A[$0] = 1; next} !A[$0]' OFS="," $1 $2 > resultfile.csv

But my resultant file shows:


Code:
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B

I need it to show me this:

Code:
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,5,5,1,CELL,1A,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B

What am I doing wrong?
Sponsored Links
    #2  
Old 04-07-2010
grepFruit grepFruit is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 15 March 2011, 5:14 AM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

The diff utility will compare the contents of file1 and file2 and write to standard output a list of changes necessary to convert file1 into file2:

Code:
$ diff file1 file2
5,6c5,6
< Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,5,5,1,CELL,1A,,,,ERICSSON,GSM,07B
< Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,5,5,1,CELL,1A,,,,ERICSSON,GSM,07B
---
> Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B
> Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B

or another command sdiff


Code:
$ sdiff file1 file2
User Parameter,Entity Name,Cell ID,Type SubCell / TX / FHSY /      User Parameter,Entity Name,Cell ID,Type SubCell / TX / FH
User Parameter,Entity Name,Cell ID,Type SubCell / TX / FHSY /      User Parameter,Entity Name,Cell ID,Type SubCell / TX / FH
,,,,,,,,,,,,,,                                                     ,,,,,,,,,,,,,,
,,,,,,,,,,,,,,                                                     ,,,,,,,,,,,,,,
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,5,5,1,CELL,1A,,   |  Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,5,5,1,CELL,1A,,   |  Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,
Access Grant Blocks Reserved,CELL,1B,,,,AGBLK,2,2,1,CELL,1B,,      Access Grant Blocks Reserved,CELL,1B,,,,AGBLK,2,2,1,CELL,
Access Grant Blocks Reserved,CELL,1B,,,,AGBLK,2,2,1,CELL,1B,,      Access Grant Blocks Reserved,CELL,1B,,,,AGBLK,2,2,1,CELL,
Access Grant Blocks Reserved,CELL,1C,,,,AGBLK,3,3,1,CELL,1C,,      Access Grant Blocks Reserved,CELL,1C,,,,AGBLK,3,3,1,CELL,
Access Grant Blocks Reserved,CELL,1C,,,,AGBLK,3,3,1,CELL,1C,,      Access Grant Blocks Reserved,CELL,1C,,,,AGBLK,3,3,1,CELL,
Access Grant Blocks Reserved,CELL,2A,,,,AGBLK,7,7,1,CELL,2A,,      Access Grant Blocks Reserved,CELL,2A,,,,AGBLK,7,7,1,CELL,
Access Grant Blocks Reserved,CELL,2A,,,,AGBLK,7,7,1,CELL,2A,,      Access Grant Blocks Reserved,CELL,2A,,,,AGBLK,7,7,1,CELL,
Access Grant Blocks Reserved,CELL,2B,,,,AGBLK,4,4,1,CELL,2B,,      Access Grant Blocks Reserved,CELL,2B,,,,AGBLK,4,4,1,CELL,
Access Grant Blocks Reserved,CELL,2B,,,,AGBLK,4,4,1,CELL,2B,,      Access Grant Blocks Reserved,CELL,2B,,,,AGBLK,4,4,1,CELL,
Access Grant Blocks Reserved,CELL,2C,,,,AGBLK,0,0,1,CELL,2C,,      Access Grant Blocks Reserved,CELL,2C,,,,AGBLK,0,0,1,CELL,
Access Grant Blocks Reserved,CELL,2C,,,,AGBLK,0,0,1,CELL,2C,,      Access Grant Blocks Reserved,CELL,2C,,,,AGBLK,0,0,1,CELL,
Access Grant Blocks Reserved,CELL,3A,,,,AGBLK,1,1,1,CELL,3A,,      Access Grant Blocks Reserved,CELL,3A,,,,AGBLK,1,1,1,CELL,
Access Grant Blocks Reserved,CELL,3A,,,,AGBLK,1,1,1,CELL,3A,,      Access Grant Blocks Reserved,CELL,3A,,,,AGBLK,1,1,1,CELL,
Access Grant Blocks Reserved,CELL,3B,,,,AGBLK,6,6,1,CELL,3B,,      Access Grant Blocks Reserved,CELL,3B,,,,AGBLK,6,6,1,CELL,
Access Grant Blocks Reserved,CELL,3B,,,,AGBLK,6,6,1,CELL,3B,,      Access Grant Blocks Reserved,CELL,3B,,,,AGBLK,6,6,1,CELL,
Access Grant Blocks Reserved,CELL,3C,,,,AGBLK,4,4,1,CELL,3C,,      Access Grant Blocks Reserved,CELL,3C,,,,AGBLK,4,4,1,CELL,
Access Grant Blocks Reserved,CELL,3C,,,,AGBLK,4,4,1,CELL,3C,,      Access Grant Blocks Reserved,CELL,3C,,,,AGBLK,4,4,1,CELL,
$

with for example

Code:
$ comm -13 file1 file2
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B
$

you can see, what in file2, but not in the file1.

Is this, what you mean?

Greeting
Sponsored Links
    #3  
Old 04-07-2010
ladyAnne ladyAnne is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 9 September 2011, 1:25 AM EDT
Posts: 83
Thanks: 10
Thanked 0 Times in 0 Posts
Yes, to make it simpler, I just need to print the line of where the two files differ, and that must be printed to another file. Perhaps, where File2 differs from file1, and print the results of file 2 to the resultant file.

How would that be possible?
    #4  
Old 04-07-2010
grepFruit grepFruit is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 15 March 2011, 5:14 AM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
 
$ comm -13 file1 file2 > file3
$ cat file3
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B
Access Grant Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B
$

Sponsored Links
    #5  
Old 04-07-2010
ladyAnne ladyAnne is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 9 September 2011, 1:25 AM EDT
Posts: 83
Thanks: 10
Thanked 0 Times in 0 Posts
Thank you so much. Suppose I wanted to list the line from the old file (the original data) along with what has changed in the second file, hwo can I do that?
Sponsored Links
    #6  
Old 04-07-2010
grepFruit grepFruit is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 15 March 2011, 5:14 AM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
how will that look? Small example please
Sponsored Links
    #7  
Old 04-07-2010
ladyAnne ladyAnne is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 9 September 2011, 1:25 AM EDT
Posts: 83
Thanks: 10
Thanked 0 Times in 0 Posts
For example say suppose these were my files:

file1:


Code:
 
Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B
Blocks Reserved,CELL,1B,,,,AGBLK,2,2,1,CELL,1B,,,,ERICSSON,GSM,07B
Blocks Reserved,CELL,1C,,,,AGBLK,3,3,1,CELL,1C,,,,ERICSSON,GSM,07B

file2:


Code:
 
Blocks Reserved,CELL,1A,,,,AGBLK,5,5,1,CELL,1A,,,,ERICSSON,GSM,07B
Blocks Reserved,CELL,1B,,,,AGBLK,2,2,1,CELL,1B,,,,ERICSSON,GSM,07B
Blocks Reserved,CELL,1C,,,,AGBLK,3,3,1,CELL,1C,,,,ERICSSON,GSM,07B

Then I want my result file to display the original line from file1 and below that the line that changed in file2:


Code:
 
Blocks Reserved,CELL,1A,,,,AGBLK,0,0,1,CELL,1A,,,,ERICSSON,GSM,07B
Blocks Reserved,CELL,1A,,,,AGBLK,5,5,1,CELL,1A,,,,ERICSSON,GSM,07B

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare two files, diff the result jimmy_y Shell Programming and Scripting 6 02-11-2010 11:13 AM
Compare two files and output diff to third file altamaha Shell Programming and Scripting 8 09-25-2008 10:42 AM
compare 2 coloum of 2 diff files using perl script vasuki Shell Programming and Scripting 3 07-30-2008 01:53 AM
Compare/Diff between directories and subdirectories? andylee80 UNIX for Dummies Questions & Answers 5 07-12-2007 09:45 AM
diff 2 files; output diff's to 3rd file blt123 Shell Programming and Scripting 2 05-28-2002 11:29 AM



All times are GMT -4. The time now is 05:13 AM.