Merge the three csv files as one according to first coloumn.


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Merge the three csv files as one according to first coloumn.
# 1  
Old 06-24-2019
Merge the three csv files as one according to first coloumn.

I have three files with similar pattern i need to merge all the coloumns side by side from all three files according to the first coloumn example as shown below
I mentioned 5 coloumns only in example but i have around 15 coloumns in each file.

file1:

Code:
Name,Samples,Error,95RT,90RT
GETcategoryCurrentData,15241,152,0.00%,42
GETproduction-Recommendations,23690,179,0.00%,456
GETstockStatusSkuID,26426,752,0.00%,442
GEThomeCSS,28683,602,0.00%,426


file2:

Code:
Name,Samples,Error,95RT,90RT
GETcategoryCurrentData,16241,175,0.00%,47
GETproduction-Recommendations,27090,181,0.00%,490
GETstockStatusSkuID,20006,750,0.00%,440
GEThomeCSS,20613,610,0.00%,430


file3:
Code:
Name,Samples,Error,95RT,90RT
GETcategoryCurrentData,75241,167,0.00%,49
GETproduction-Recommendations,78390,190,0.00%,789
GETstockStatusSkuID,66826,790,0.00%,700
GEThomeCSS,21809,612,0.00%,789



sample output:

Code:
Name,Samples,Samples,Samples,Error,Error,Error,95RT,95RT,95RT,90RT,90RT,90RT
GETcategoryCurrentData,15241,16241,75241,152,175,167,0.00%,0.00%,0.00%,42,47,49
GETproduction-Recommendations,23690,27090,78390,179,181,190,0.00%,0.00%,0.00%,456,490,789
GETstockStatusSkuID,26426,27090,78390,752,750,790,0.00%,0.00%,0.00%,442,440,700
GEThomeCSS,28683,20613,21809,602,610,612,0.00%,0.00%,0.00%,426,430,789

could any one help me on this.

Last edited by Raghuram717; 06-24-2019 at 08:50 AM..
# 2  
Old 06-24-2019
Try:
Code:
awk '
  (getline f<fn)>0 && (getline g<gn)>0 {
    split(f,F)
    split(g,G)
    for(i=2; i<=NF; i++)
      $i=$i OFS F[i] OFS G[i]
    print
  }
' FS=, OFS=, fn=file2 gn=file3 file1

# 3  
Old 06-25-2019
Above script worked but it is adding first line from the first file,second file, third file as one..
i need it as it should go check go look for ex:"GETcategoryCurrentData" from three files and write it as one row.
i can use sort but if rows are different from one file to other file then they may merge wrongly.

could you please help me in this scenario.

file1:
# 4  
Old 06-25-2019
Try this for merging data according to the field 1 values, trying to keep the order of lines to the first file's. It partly takes care of $1 values disappearing or newly occurring, but does not care for the order those appear in the output lines. For more convoluted input cases, additional measures need to be taken.
Code:
awk '
!F[$1]  {F[$1]    = $1
         C[++CNT] = $1
        }
        {for (i=2; i<=NF;i++) T[$1,i] = T[$1,i] OFS $i
        }
END     {for (j=1; j<=CNT; j++) {printf "%s", C[j]
                                 for (i=2; i<=NF; i++) printf "%s", T[C[j],i]
                                 printf ORS
                                }
        } 
 ' FS=, OFS=, file[1-3]


Be aware that your sample output file does NOT correctly reflect the input data!
This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-26-2019
Thank You..It worked...!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare and merge two big CSV files

Hi all, i need help. I have two csv files with a huge amount of data. I need the first column of the first file, to be compared with the data of the second, to have at the end a file with the data not present in the second file. Example File1: (only one column) profile_id 57036226... (11 Replies)
Discussion started by: SirMannu
11 Replies

2. UNIX for Beginners Questions & Answers

Replace in the specific coloumn of a csv

I have a CSV with 6 coloumn. I need to replace a ',' with '|' in the 4th coloumn of a CSV only so i can upload in Database. When i Upload now, commas used in the text in the 4th coloumn are treated as delimiter and remaining text goes to the next coloumn. Any solutions to replace, thanks. (1 Reply)
Discussion started by: adnan11
1 Replies

3. Shell Programming and Scripting

I am trying to merge all csv files from source path into 1 file

I am trying to merge all csv files from source path into one single csv file in target. but getting error message: hadoop fs -cat /user/hive/warehouse/stage.db/PK_CLOUD_CHARGE/TCH-charge_*.csv > /user/hive/warehouse/stage.db/PK_CLOUD_CHARGE/final/TCH_pb_charge.csv getting error message:... (0 Replies)
Discussion started by: cplusplus1
0 Replies

4. UNIX for Dummies Questions & Answers

Merge two csv files using column name

Hi all, I have two separate csv files(comma delimited) file 1 and file 2. File 1 contains PAN,NAME,Salary AAAAA5467D,Raj,50000 AAFAC5467D,Ram,60000 BDCFA5677D,Kumar,90000 File 2 contains PAN,NAME,Dept,Salary ASDFG6756T,Karthik,ABC,450000 QWERT8765Y,JAX,CDR,780000... (5 Replies)
Discussion started by: Nivas
5 Replies

5. Shell Programming and Scripting

Merge CSV files

I have lot of csv file collected from script like below : Name of files (some examples) there are thousands of it: 192.168.0.123_251_18796_1433144473.csv 192.168.0.123_251_18796_1433144772.csv 192.168.0.123_251_18796_1433145073.csv 192.168.0.123_251_18796_1433145372.csvContent of each... (5 Replies)
Discussion started by: rk4k
5 Replies

6. UNIX for Dummies Questions & Answers

Need help combining txt files w/ multiple lines into csv single cell - also need data merge

:confused:Hello -- i just joined the forums. I am a complete noob -- only about 1 week into learning how to program anything... and starting with linux. I am working in Linux terminal. I have a folder with a bunch of txt files. Each file has several lines of html code. I want to combine... (2 Replies)
Discussion started by: jetsetter
2 Replies

7. Shell Programming and Scripting

Merge *.csv files, each in separate sheets

Does anyone know how to Merge *.csv files, each in seperate sheets? (7 Replies)
Discussion started by: frhling
7 Replies

8. Shell Programming and Scripting

Merge CSV files and create a column with the filename from the original file

Hello everyone!! I am not completely new to shell script but I havent been able to find the answer to my problem and I'm sure there are some smart brains here up for the challenge :D. I have several CSV files that I need to combine into one, but I also need to know where each row came from.... (7 Replies)
Discussion started by: fransanchezoria
7 Replies

9. Shell Programming and Scripting

Merge 2 CSV files using sed

Help in writing a script using sed which updates fileOne with the contents from fileTwo Example: Contents of fileOne 1,111111 2,897823 3,235473 4,222222 Contents of fileTwo 1,111111,A,1,2 4,222222,A,2,2 5,374632,A,3,2 6,374654,A,4,2 Final File should be: 1,111111,A,1,2... (9 Replies)
Discussion started by: NewToSed
9 Replies

10. Shell Programming and Scripting

Merge 2 csv files with awk

I have 2 files pipe delimted and want to merge them based on a key e.g file 1 123$aaa$yyy$zzz 345$xab$yzy$zyz 456$sss$ttt$foo 799$aaa$ggg$dee file 2 123$hhh 345$ddd 456$xxx 888$zzz so if the key is the first field, and the result should be the common key between file 1 and 2 (6 Replies)
Discussion started by: loloAix
6 Replies
Login or Register to Ask a Question