Help with merging two CSV files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with merging two CSV files
# 1  
Old 07-03-2012
Help with merging two CSV files

Hi,

I have following 2 CSV files

Code:
  
file1.txt
A1,B1,C1,D1,E1
A2,B2,C2,D2,E2
A3,B3,C3,D3,E3
....
 
file2.txt
A1,B1,P1,Q1,R1,S1,T1,U1
A1,B1,P2,Q2,R2,S2,T2,U2
A1,B1,P3,Q3,R3,S3,T3,U3
A2,B2,X1,Y1,Z1,I1,J1,K1
A2,B2,X2,Y2,Z2,I2,J2,K2
A2,B2,X3,Y3,Z3,I3,J3,K3
A2,B2,X4,Y4,Z4,I4,J4,K4
...

Only first 2 columns of the files match
file1.txt is smaller containig only one row per first 2 columns combination (A1,B1 ... A2,B2 ... only 1 row per combination)
file2.txt is bigger file with many rows per first 2 columns combinations (A1,B1 ... A2,B2 ... multiple rows per combination)

What i want to so is to combine 2 files such that per first 2 column combination in file2.txt it appends (with comma as separator) the corresponding data from file1.txt (minus the matching columns from file1.txt)

Code:
 
Example:
the output i want from above files is:
A1,B1,P1,Q1,R1,S1,T1,U1,C1,D1,E1
A1,B1,P2,Q2,R2,S2,T2,U2,C1,D1,E1
A1,B1,P3,Q3,R3,S3,T3,U3,C1,D1,E1
A2,B2,X1,Y1,Z1,I1,J1,K1,C2,D2,E2
A2,B2,X2,Y2,Z2,I2,J2,K2,C2,D2,E2
A2,B2,X3,Y3,Z3,I3,J3,K3,C2,D2,E2
A2,B2,X4,Y4,Z4,I4,J4,K4,C2,D2,E2
...

where data in red is from file2.txt and in blue from file1.txt

the output has each line from file2.txt (ex A1,B1,P1,Q1,R1,S1,T1,U1) and after comma the corresponding data from file1.txt (C1,D1,E1) without the matching column (A1,B1)

I do not want to use loop as it will be time consuming since the files are huge.

i used below awk cmd, but it doesn't give me desired output.
Code:
 
nawk -F"," 'FNR==NR{f1[$1,$2]=$0;next}{idx=$1 SUBSEP $2; if(idx in f1) $0=f1[idx] OFS $0}1' file1.txt file.txt

Smilie

can someone pleas help me with this. Smilie

Thanks in advance.


Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by learnoutmore99; 07-03-2012 at 04:51 AM.. Reason: code tags
# 2  
Old 07-03-2012
Try:
Code:
awk -F, 'NR==FNR{for (i=3;i<=NF;i++) a[$1","$2]=a[$1","$2]","$i;next}{$0=$0""a[$1","$2]}1' file1.txt file2.txt

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-03-2012
Help with merging two CSV files

gr888888
Thanks a lot, it worked.
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging fields in CSV

Hi experts, I have a csv file which has one field (ID) repeated multiple times with corresponding other field values. I need to convert this file in a format where for a ID all other values has to be present in single field. For Eg : Here in below file ID 1 is repeated 3 times with different... (7 Replies)
Discussion started by: bharathbangalor
7 Replies

2. Shell Programming and Scripting

Merging Very large CSV files in Unix

Hi, I have two very large CSV files, which I want to merge (equi-join) based on a key (column). One of the file (say F1) would have ~30 MM records and 700 columns. The other file (~f2) would have same # of records and lesser columns (say 50). I want to create an output file joining on a... (3 Replies)
Discussion started by: student_007
3 Replies

3. Shell Programming and Scripting

Merging CSV fields based on a common field

Hi List, I have two files. File1 contains all of the data I require to be processed, and I need to add another field to this data by matching a common field in File2 and appending a corresponding field to the data in File1 based on the match... So: File 1:... (1 Reply)
Discussion started by: landossa
1 Replies

4. Shell Programming and Scripting

Merging all (48) CSV files from a directory

I have 48 csv files in my directory that all have this form: Storm Speed (mph),43.0410781151 Storm motion direction (degrees),261.580774982 MLCAPE,2450.54098661 MLCIN,-9.85040520279 MLLCL,230 MLLFC,1070.39871 MLEL,207.194689294 MLCT,Not enough data Sbcape,2203.97617778... (3 Replies)
Discussion started by: RissaR
3 Replies

5. Shell Programming and Scripting

Matching lines across multiple csv files and merging a particular field

I have about 20 CSV's that all look like this: "","","","","","","","","","","","","","","",""What I've been told I need to produce is the exact same thing, but with each file now containing the start_code from every other file where the email matches. It doesn't matter if any of the other... (1 Reply)
Discussion started by: Demosthenes
1 Replies

6. UNIX for Dummies Questions & Answers

Merging two CSV files by 3 primary keys (columns)

Hi there! I have the following problem: I have a set of files called rates_op_yyyyddmm with the format below (which corresponds to the file rates_op_20090130) 30-JAN-2009,ED,FEB09,C,96.375,,,0,,,,,,2.375,,,,,, 30-JAN-2009,ED,FEB09,C,96.5,,,0,,,,,,2.25,,,,,,... (2 Replies)
Discussion started by: Pep Puigvert
2 Replies

7. Shell Programming and Scripting

Merging files to create CSV file

Hi, I have different files of the same type, as: Time: 100 snr: 88 perf: 10 other: 222 Each of these files are created periodically. What I need to do is to merge all of them into one but having the following form: (2 Replies)
Discussion started by: Ravendark
2 Replies

8. UNIX for Dummies Questions & Answers

Merging Non-Empty Columns within a CSV

I am trying to place all my data in a single row (order doesn't matter). Note I am a Unix novice, so please go easy on me. Here is an example Raw data: row# (1) 45 64 23 (2) 32 1 6 56 (3) 32 45 Needs to be like this: row# (1) 45 (2) 32 (3) 32 ... (2 Replies)
Discussion started by: mmann1123
2 Replies

9. UNIX for Dummies Questions & Answers

Merging 2 .CSV files in Unix

I need a little help as I am a complete novice at scripting in unix. However, i am posed with an issue...:eek: i have two csv files in the following format@ FILE1.CSV: HEADER HEADER Header , , HEADER 001X ,,200 002X ,,300 003X ... (6 Replies)
Discussion started by: chachabronson
6 Replies

10. Shell Programming and Scripting

merging CSV data using a one liner from shell?

I'm trying to merge multiple CSV (comma separated value) files into one large master file. All files have a field that is unique to act as the key for entry/merging into the master file & and all files have the same number of fields that are in the master file. I'll give an example here: ... (2 Replies)
Discussion started by: jjinca
2 Replies
Login or Register to Ask a Question