The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare two files charandevu Shell Programming and Scripting 7 03-30-2008 12:20 PM
Compare files kharen11 UNIX for Advanced & Expert Users 25 03-14-2007 01:35 AM
compare two txt files space13 Shell Programming and Scripting 8 09-22-2006 06:40 AM
compare files and beyond MizzGail UNIX for Dummies Questions & Answers 2 04-25-2003 10:34 AM
compare files ingunix UNIX for Dummies Questions & Answers 3 05-24-2001 08:44 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-09-2007
Registered User
 

Join Date: Jan 2006
Posts: 71
compare 2 files..

Hello..I am trying to find solution for this:

I have 2 files

A and B

A:

1234
2345
3211
1111
5555
6666
7777
8888
9876


B:

1234
2345
3211
1111
5555
6666
9988
8899
2244


output should be:

9988
8899
2244

becouse that is difference between files.

I tryed with diff...also I did something with sdiff and then grep "\>" U know to see just lines that are in certain file...

can U help me with better solution..maybe awk??

thanks a lot..
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-09-2007
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 371
Try:
Code:
comm -13 f1 f2
Cheers
Reply With Quote
  #3 (permalink)  
Old 03-09-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
awk ' 
BEGIN { 
while ( getline < "A" ) { arr[$0]=1 } 
} 
{ if ( arr[$0]  ~ /^ *$/ )  print FILENAME":" $0  
  else delete arr[$0]; 
}
END { 
for( key in arr ) 
if ( key !~ /^ *$/ && arr[key] == 1) print "A:" key
} ' B
Code:
grep -vf A B
grep -vf B A
A.sorted and B.sorted contain sorted files of A and B
Code:
comm -23 A.sorted B.sorted
comm -13 A.sorted B.sorted
Reply With Quote
  #4 (permalink)  
Old 03-09-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,573
Code:
#! /opt/third-party/bin/perl

open(FILE, "<", "a" ) || die "Unable to open file a <$!>\n";

while ( <FILE> ) {
  chomp;
  $fileHash{$_} = $i++;
}

close(FILE);

open(FILE, "<", "b" ) || die "Unable to open file a <$!>\n";

while( <FILE> ) {
  chomp;
  if( exists $fileHash{$_} ) {
  }
  else {
    print "$_\n";
  }
}

close(FILE);

exit 0
Reply With Quote
  #5 (permalink)  
Old 03-09-2007
Registered User
 

Join Date: Jan 2006
Posts: 71
T h a n k Y o u

thanks for multiple solution..

It worked very well..

I wish I cen get anbu solution explanation...I know basics in awk..but I got confused with arrays..

Thanks in advance..

Cheers..
Reply With Quote
  #6 (permalink)  
Old 03-09-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
I have made few changes to the awk code.
Code:
awk ' 
BEGIN { 
while ( getline < "A" ) { arr[$0]=1 } 
} 
{ if ( arr[$0]  != 1 )  print FILENAME":" $0  
  else delete arr[$0]; 
}
END { 
for( key in arr ) 
if ( arr[key] == 1 ) print "A:" key
} ' B
Code:
while ( getline < "A" ) { arr[$0]=1 }
Here we read line by line from file A and assign one to array with the line read from file as key ( or index ).

Code:
{ if ( arr[$0]  != 1 )  print FILENAME":" $0  
  else delete arr[$0]; 
}
Here we are checking that line read from file B is present in array arr. If condition arr[$0] != 1 is satisfied which means that line is not available in file A and print or else both the file contain this line and delete it from array arr.

Code:
END { 
for( key in arr ) 
if ( arr[key] == 1 ) print "A:" key
}
Since we have deleted whatever is common to both files and print the remaining lines which is present only in file A.
Reply With Quote
  #7 (permalink)  
Old 03-09-2007
Registered User
 

Join Date: Jan 2006
Posts: 71
thanks again for explanation...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:23 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0