awk comparison using multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk comparison using multiple files
# 1  
Old 02-12-2018
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:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-12-2018 at 11:38 AM.. Reason: Added CODE tags.
# 2  
Old 02-12-2018
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

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-13-2018
Thanks its working as per expectation.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Replacement using 2 diff files input and comparison

Requirement: If $5(date field) in ipfile is less than $7(date field) in deact file & $1 of ipfile is present in deactfile then $1 to be replaced by $2,$3,$4,$5,$6 of deact file else if $5(date field) in ipfile is greater than $7(date field) in actfile & $1 of ipfile is present in actfile then... (5 Replies)
Discussion started by: siramitsharma
5 Replies

2. Shell Programming and Scripting

Comparison of multiple files

Need a write script in bournce shell. Compare all the file contents and need to generate a report. Example : Having 10 trace.log files like below trace1.log, trace2.log .... trace10.log Need to compare all the 10 files contents and provide the report as below, Assume trace... (1 Reply)
Discussion started by: sureshmani
1 Replies

3. Shell Programming and Scripting

comparison of 2 files using unix or awk

Hello, I have 2 files and I want them to be compared in a specific fashion file1: A_1200_1250 A_1251_1300 B_1301_1350 B_1351_1400 B_1401_1450 C_1451_1500 and so on... file2: 1210 1305 1260 1295 1400 1500 1450 1495 Now The script should look for "1200" from A_1200_1250 of... (8 Replies)
Discussion started by: Diya123
8 Replies

4. UNIX for Dummies Questions & Answers

df -> output files; comparison using awk or...

:wall: I am trying to do the following using awk (is that the best way?): Read 2 files created from the output of df (say, on different days) and compare the entries using the 1st (FileSys) and 6th (Mount) fields to see if the size has changed. Output (at least), to a new file (some header... (2 Replies)
Discussion started by: renata
2 Replies

5. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

6. Shell Programming and Scripting

Awk multiple variable array: comparison

Foo.txt 20 40 57 50 22 51 66 26 17 15 63 18 80 46 78 99 87 2 14 14 51 47 49 100 58 Bar.txt 20 22 51 15 63 78 99 55 51 58 How to get output using awk 20 22 57 50 51 15 26 17 63 78 80 46 99 55 - - 51 58 49 100 (5 Replies)
Discussion started by: genehunter
5 Replies

7. UNIX for Dummies Questions & Answers

multiple comparison in awk

I have an input file. Each line in it has several characters. If the first three characters of the line is '000' or '001' or '002' or '003', I need to print it in output. How can I do this in awk. I am able to do if the search string is only one (let us say 000). cat <filename> | awk... (1 Reply)
Discussion started by: paruthiveeran
1 Replies

8. Shell Programming and Scripting

Awk Comparison of 2 specific files

Hi Everybody, I know the topic sounds familiar but I just couldn't adapt or find the right code that solves my particular issue. I really hope you can help. I would like to compare 2 files in an awk script. Both files have different paths. The awk script call should look like that awk -f... (7 Replies)
Discussion started by: hhoosscchhii
7 Replies

9. Shell Programming and Scripting

Comparison of two files in awk

Hi, I have two files file1 and file2 delimited by semicolon, And I want to compare column 2 and column3 of file1 to column3 and column 4 in file2. file1 -------- abc;cef;155.67;143_34; def;fgh;146.55;123.3; frg;hff;134.67;; yyy;fgh;134.78;35_45; file 2 --------- abc;cef;155.09;;... (12 Replies)
Discussion started by: jerome Sukumar
12 Replies

10. Shell Programming and Scripting

String Comparison between two files using awk

I have two files with field seperator as "~". File A: 12~13~14~15 File B: 22~22~32~11 i want to calculate the difference between two files and than calculate the percentage difference and output it to a new file. How do i do this using awk. Also please suggest GOOD awk tutorials. Thank... (7 Replies)
Discussion started by: rudoraj
7 Replies
Login or Register to Ask a Question