The UNIX and Linux Forums  

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



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #6 (permalink)  
Old 03-09-2007
anbu23 anbu23 is offline
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