Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

awk comparison using multiple files

UNIX for Beginners Questions & Answers


Tags
awk, solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago   -   Original Discussion by grv
grv's Unix or Linux Image
grv grv is offline
Registered User
 
Join Date: Feb 2018
Last Activity: 13 February 2018, 9:38 AM EST
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
awk comparison using multiple files

Hi,

I have 2 files, I need to use column of file1 and do a comparison on file2 column 1 and print the mismatch is file3 as mentioned below.

Kindly consider that file 1 is having uniq key(column) whereas in file2 we have multiple duplicates (like 44). These duplicates should not come in output of file 3 but should be routed to a new file4.


file1:



Code:
1,apple  
2,mango  
3,banana  
44,orange

file2:



Code:
1,apple  
22,  
31,xyz  
2,man  
3,banana  
44,oran   
44,orange

The expected output to file file3 :-



Code:
2,mango,man

and in file4 we should capture duplicates :-


Code:
44,oran   
44,orange

Through different forum i got the cmd as {

Code:
awk 'BEGIN{FS=OFS=","}($1 in a) && a[$1]!=$2{print $1,a[$1],$2}{a[$1]=$2}' file1 file2 >> file3

}

for file 3 generation but it is not working fine with duplicates.


Moderator's Comments:
awk comparison using multiple files Please use CODE tags as required by forum rules!

Last edited by RudiC; 1 Week Ago at 11:38 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago   -   Original Discussion by grv
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 February 2018, 3:15 PM EST
Location: Aachen, Germany
Posts: 12,167
Thanks: 371
Thanked 3,752 Times in 3,447 Posts
Welcome to the forum.

Please become accustomed to carefully phrase your question / request. There's some guesses necessary to understand it:
  • "use column of file1" means column 1, doesn't it?
  • And, "print the mismatch" means mismatch between fields 2 in the files?
  • Duplicates should be printed regardless of matches ("orange") or mismatches ("oran")?


Howsoever, see if my assumptions are correct and try


Code:
awk -F, -vOFS="," '
NR == FNR       {T[$1] = T[$1] ORS $0
                 next
                }

$1 in T         {sub ("^" ORS, "", T[$1])
                 n = split (T[$1], X)
                 if (n == 2)    {if ($2 != X[2]) print $0, X[2] > "file3"
                                }
                   else          print T[$1]  > "file4" 
                }
' file2 file1

The Following User Says Thank You to RudiC For This Useful Post:
grv (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago   -   Original Discussion by grv
grv's Unix or Linux Image
grv grv is offline
Registered User
 
Join Date: Feb 2018
Last Activity: 13 February 2018, 9:38 AM EST
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks its working as per expectation.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Comparison of multiple files sureshmani Shell Programming and Scripting 1 10-19-2011 05:22 PM
Comparison and editing of files using awk.(And also a possible bug in awk for loop?) linuxkid Shell Programming and Scripting 1 07-22-2010 05:37 AM
Awk multiple variable array: comparison genehunter Shell Programming and Scripting 5 05-01-2010 12:25 AM
multiple comparison in awk paruthiveeran UNIX for Dummies Questions & Answers 1 06-17-2009 06:49 PM
Comparison of two files in awk jerome Sukumar Shell Programming and Scripting 12 07-26-2006 09:16 AM



All times are GMT -4. The time now is 04:07 AM.