×
UNIX.COM Login
Username:
Password:  
Show Password






👤


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

Tags
awk, solved

👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-12-2018
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; 02-12-2018 at 10:38 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old 02-12-2018
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 16 July 2018, 3:46 PM EDT
Location: Aachen, Germany
Posts: 13,062
Thanks: 447
Thanked 4,011 Times in 3,688 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 (02-13-2018)
Sponsored Links
    #3  
Old 02-13-2018
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
👤 Login to reply

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
Comparison of multiple files sureshmani Shell Programming and Scripting 1 10-19-2011 04: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 04:37 AM
Awk multiple variable array: comparison genehunter Shell Programming and Scripting 5 04-30-2010 11:25 PM
multiple comparison in awk paruthiveeran UNIX for Dummies Questions & Answers 1 06-17-2009 05:49 PM
Comparison of two files in awk jerome Sukumar Shell Programming and Scripting 12 07-26-2006 08:16 AM



All times are GMT -4. The time now is 12:08 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.