Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have 3 csv files like this.

csv 1:

1,aaaa,bbb,2014-04-01
2,qwe,rty,2014-04-03
3,zxc,cvb,2014-04-05

csv 2:

2,j,k,2014-04-01
3,a,s,2014-04-04
5,g,h,2014-04-08

csv 3:

2,a,s,d,f,g,2014-04-01
3,d,f,g,h,j,2014-04-06
4,c,v,b,n,m,2014-04-09

How can I merge all by the first column?

SELECT * FROM csv1
JOIN csv2 where csv1[0]= csv2[0] --[0] is the position of the first column

The output should be:

 csv1 fields     | csv2 fields |  csv4 fields

 2,qwe,rty,2014-04-03,a,s,2014-04-04,a,s,d,f,g,2014-04-01
 3,zxc,cvb,2014-04-05,g,h,2014-04-08,d,f,g,h,j,2014-04-06  
share|improve this question
    
Your desired output appears to mix up the values e.g. line 3 has g,h from line 5 of csv2 - is that what you intended? And what is csv4? –  steeldriver Jul 10 at 15:21
    
Just found a SQL engine over CSV files: github.com/harelba/q –  LatinSuD Jul 10 at 15:22
    
Please don't ask multiple questions in a single post. Post a separate question for each issue instead. Since I see that you have posted your 2nd question separately, I am deleting it from here. –  terdon Jul 12 at 11:46

1 Answer 1

You should be able to do the basic merge using two applications of the coreutils join command if your system has it. If your shell supports process substitution you can do that as

join -t, <(join -t, csv1 csv2) csv3

(otherwise you may need to use an intermediate temporary file). Using your csv1, csv2 and csv3 files as posted, that gives:

$ join -t, <(join -t, csv1 csv2) csv3
2,qwe,rty,2014-04-03,j,k,2014-04-01,a,s,d,f,g,2014-04-01
3,zxc,cvb,2014-04-05,a,s,2014-04-04,d,f,g,h,j,2014-04-06

To compare dates (at least lexically), you could pipe the result via awk e.g.

join -t, <(join -t, csv1 csv2) csv3 | awk -F, '$NF!=$4'
share|improve this answer
    
user3696932 did post an answer instead of a comment to comment your answer. Please take a look at the updated, edited question containing this comment, if you already haven't done so. –  polym Jul 12 at 11:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.