Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How to compare two columns in two files? Post 302969029 by PRAMOD 96 on Thursday 17th of March 2016 08:24:33 AM
Old 03-17-2016
update file1 using file2 in awk

Hi all,
I have file a.dat with content
Code:
0,ID,,
1,001,2015060,UB0085000,Key2,abc,xyz
1,338,2015103,UB0085000,Key1,abc,xyz
1,338,2015103,UB0085000,Key5,abc,xyz
1,338,20017457,UB0085000,Key3,abc,xyz
1,338,20017457,UB0085000,Key4,abc,xyz
9,9,2016-03-17-09.03.13.131313

and file b.dat
Code:
1,Key1,a.txt,b.txt,c.txt
1,Key3,x.txt,y.txt,z.txt
1,Key5,p.txt,q.txt,r.txt
1,Key2,l.txt,m.txt,n.txt
1,Key4,j.txt,k.txt,l.txt
9,9,2016-03-17-09.03.13.131313

Final output should be(Print 1st and last line of a.dat as it is)
Code:
1,Key1,a.txt,b.txt,c.txt
1,Key3,x.txt,y.txt,z.txt
1,Key5,p.txt,q.txt,r.txt
1,Key2,l.txt,m.txt,n.txt
1,Key4,j.txt,k.txt,l.txt
9,9,2016-03-17-09.03.13.131313

Requirement :
I have to update file a.dat with help of b.dat having condition that search 4th column value of a.dat in b.dat. If matches then update column 6,7,8 of a.dat with the values of column 3,4,5 of b.dat and shift existing column 6,7,8 value
to next of a.dat

My Code :
Code:
awk 'NR==FNR{A[$2]=$3; B[$2]=$4; C[$2]=$5; next} {$6=A[$5]; $7=B[$5]; $8=C[$5]}1' FS=, OFS=, b.dat a.dat

My Output :
Code:
0,ID,,,,,,
1,001,2015060,UB0085000,Key2,l.txt,m.txt,n.txt
1,338,2015103,UB0085000,Key1,a.txt,b.txt,c.txt
1,338,2015103,UB0085000,Key5,p.txt,q.txt,r.txt
1,338,20017457,UB0085000,Key3,x.txt,y.txt,z.txt
1,338,20017457,UB0085000,Key4,j.txt,k.txt,l.txt
9,9,2016-03-17-09.03.13.131313,,,,,

Current Problem
1st and last line also get proccessed and contain many `,`
existing values of a.dat of column 6,7,8 is not shifting next.

Please suggest solution to this problem ..

Last edited by Don Cragun; 06-09-2016 at 02:37 AM.. Reason: Remove two duplicate posts, add CODE and ICODE tags, again.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare few columns from two files

My Friends, Need your help to find the difference between few columns from two comma delimited files. For example, File1 and File2 has 22 columns, and I want to find the difference in first 12 columns. I have list of file names in MyListOfFiles2Compare.txt. Data is separated with commas.... (5 Replies)
Discussion started by: manish44
5 Replies

2. Shell Programming and Scripting

How to compare two columns in two files?

Hello all, Could someone please let me know shell script or awk solution to compare two columns in two files? Here is the sample - file1.txt abc/xyz,M1234 ddd/lyg,M2345 cnn/tnt,G0123 file2.txt A,abc/xyz,kk,dd,zz,DCT,G0123,1 A,ddd/lyg,kk,dd,zz,DCT,M1234,1... (17 Replies)
Discussion started by: sncoupons
17 Replies

3. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

4. UNIX for Dummies Questions & Answers

Compare Columns in two files

Hi all, I would like to compare a column in one file to a column in another file and when there is a match it prints the first column and the corresponding second column. Example File1 ABA ABC ABE ABF File 2 ABA 123 ABB 124 ABD 125 ABC 126 So what I would like printed to a file... (0 Replies)
Discussion started by: pcg
0 Replies

5. Shell Programming and Scripting

Compare Columns of two files

Hi I have file 1 like this and file 2 like this I need to compare column 3 of both files and delete lines in file1 with same column 3 values in two files. So the output is I tried with perl but didnt work. A perl code will be good as I am learning the language, but any other code would... (1 Reply)
Discussion started by: polsum
1 Replies

6. Shell Programming and Scripting

Compare columns in two different files using awk

Hi, I want to compare the columns of two files excluding column 2 from both the files. I tried this awk command. awk -F":" 'NR==FNR{++a;next} !(a)' file1.txt file2.txt . Example: File1.txt 123:09-15-2011:abc:123456 123:09-15-2011:abc:234567 123:09-15-2011:abc:345678 ... (5 Replies)
Discussion started by: shell_newbie
5 Replies

7. Shell Programming and Scripting

Compare multiple columns from 2 files

Hi, I need to compare multiple columns from 2 files. I can, for example, have these 2 files: file1: col1, col2, col3,col4 a,1,4,7 b,2,5,8 c,3,6,9file2: col1, col2, col3,col4 a,2,3,2 b,5,7,5 c,1,9,8As a result, I need for example the difference between the columns 2 and 4: col2,... (3 Replies)
Discussion started by: Subbeh
3 Replies

8. Shell Programming and Scripting

Compare columns in different files

Hi, I have two files like this: 8 1.3 10 1.3 12 1.3 15 1.3 21 1.3 and 1 2 3 4 10 11 15 16 21 22 (3 Replies)
Discussion started by: jamie_123
3 Replies

9. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

10. UNIX for Dummies Questions & Answers

Help need to compare columns in files

Hi, Below is my requirement file1 id|cnt 1|1 2|2 3|3 file2 id_1|cnt_1 1|1 2|1 3|1 I want to compare cnt and cnt_1 columns, if they are differ then give the details Am using below awk command, but the output is not as expected. (2 Replies)
Discussion started by: grandhirahuletl
2 Replies
All times are GMT -4. The time now is 02:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy