4

I have two files as given below

Output1.csv

201319107648361,12:27:04,12:27:14,0:0:10 secs
201319109089878,12:27:04,12:27:15,0:0:11 secs
201319108968961,12:27:15,12:27:54,0:0:9 secs
201319108686853,12:27:34,12:27:43,0:0:9 secs
.
.
.
n

Output2.csv

201319107648361,200
201319109089878,129
201319108968961,719
201319108686853,412
.
.
.
n

I need to merge these two files in to a single file with one condition i.e if the first field of both the file are same.

For Instance,

If the first value or field of Output1.csv - 201319107648361 is equal to the first field of Output2.csv then need to print the remaining field of the files in to another file.

Desired output file should contain:

201319107648361,12:27:04,12:27:14,0:0:10 secs,200
201319109089878,12:27:04,12:27:15,0:0:11 secs,129
201319108968961,12:27:15,12:27:54,0:0:9 secs,719
201319108686853,12:27:34,12:27:43,0:0:9 secs,412
.
.
.
n

1 Answer 1

6

All you need is join

join -t\, <(sort Output1.csv) <(sort Output2.csv)
-or-
join -t "," <(sort Output1.csv) <(sort Output2.csv)

or awk

awk -F, 'FNR==NR{a[$1]=$2;next}{ print $0 "," a[$1]}' Output2.csv Output1.csv
4
  • @Ram - ask the author, I just edited the A.
    – slm
    Oct 17, 2013 at 15:41
  • $slm:sorry for the previous comment.
    – Ram
    Oct 17, 2013 at 16:06
  • @dchirikov: It worked perfectly thanks for the help...:)
    – Ram
    Oct 17, 2013 at 16:09
  • @Ram - np, please mark this as the accepted answer so that others know the issue's been resolved.
    – slm
    Oct 17, 2013 at 16:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .