3

I have file1 likes:

6688249-7,03/02/2017,5515,02-feb-17

16914917-8,03/02/2017,5521,02-feb-17

And a file2:

5515,6688249-7,06-02-2017 04:25:47 PM,ASIG,ELIZABETH PRIETO

5515,6688249-7,06-02-2017 04:56:32 PM,URR,ELIZABETH PRIETO

5521,16914917-8,06-02-2017 12:51:04 PM,ASIGN,SOLANGE

5521,16914917-8,27-02-2017 04:32:11 PM,Ex,SOLANGE

And would like a file3 such that:

6688249-7,03/02/2017,5515,02-feb-17,06-02-2017 04:25:47 PM,ASIG,ELIZABETH PRIETO

6688249-7,03/02/2017,5515,02-feb-17,06-02-2017 04:56:32 PM,URR,ELIZABETH PRIETO

16914917-8,03/02/2017,5521,02-feb-17,06-02-2017 12:51:04 PM,ASIGN,SOLANGE

16914917-8,03/02/2017,5521,02-feb-17,27-02-2017 04:32:11 PM,Ex,SOLANGE

Which means to put the 3th,4th,5th columns of file2 to file1 by the key of the 2nd column (3th column file1, 1th column file 1)

1

Something like this should be enough:

awk 'BEGIN{FS=OFS=","}NR==FNR{a[$3]=$0;next}$1 in a{print a[$1],$3,$4,$5}' file1 file2
0

if output sort order is immaterial, this looks like a job for join

join -1 3 -2 1 -t, -o 1.1,1.2,1.3,1.4,2.3,2.4,2.5 <(sort -k3,3 -t, file1) \
    <(sort -k1,1 -t, file2)


6688249-7,03/02/2017,5515,02-feb-17,06-02-2017 04:25:47 PM,ASIG,ELIZABETH PRIETO
6688249-7,03/02/2017,5515,02-feb-17,06-02-2017 04:56:32 PM,URR,ELIZABETH PRIETO
16914917-8,03/02/2017,5521,02-feb-17,06-02-2017 12:51:04 PM,ASIGN,SOLANGE
16914917-8,03/02/2017,5521,02-feb-17,27-02-2017 04:32:11 PM,Ex,SOLANGE

The inputs to join need to be sorted on the join key, the example above uses process substitution( supported by shells such as bash) to achieve that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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