我有两个csv文件,如下所示:
name,Direction,Date
abc,sent,Jan 21 2014 02:06
xyz,sent,Nov 21 2014 01:09
pqr,sent,Oct 21 2014 03:06
和
name,Direction,Date
abc,received,Jan 22 2014 02:06
xyz,received,Nov 22 2014 02:06
我根据第一列合并这两个文件并创建合并文件。我用于所需输出的两个命令是:
awk -F, -v OFS="," 'BEGIN{print "name,Direction,Date,currentDirection,receivedDate"} NR==FNR&&NR>1{a[$1]=$0;next} FNR>1{printf "%s%s\n",$0,($1 in a?FS a[$1]:"")}' 2.csv 1.csv
join -1 1 -2 1 -t, -a 1 1.csv 2.csv | sed "s/Direction,Date/currentDirection,receivedDate/2"
这两个命令都给我以下输出:
name,Direction,Date,currentDirection,receivedDate
abc,sent,Jan 21 2014 02:06,received,Jan 22 2014 02:06
xyz,sent,Nov 21 2014 01:09,received,Nov 22 2014 02:06
pqr,sent,Oct 21 2014 03:06
但是,我想,(逗号)放在空(不匹配的数据)位置,输出应该是这样的:
name,Direction,Date,currentDirection,receivedDate
abc,sent,Jan 21 2014 02:06,received,Jan 22 2014 02:06
xyz,sent,Nov 21 2014 01:09,received,Nov 22 2014 02:06
pqr,sent,Oct 21 2014 03:06,,
请注意第三行中日期后的逗号。这是我的java应用程序读取新的csv文件所必需的。 有人可以建议我在这里缺少什么吗?
答案 0 :(得分:3)
更改awk单行(看起来像我的代码......)
awk ...... FNR>1{printf "%s%s\n",$0,($1 in a?FS a[$1]:",,") ....
实际上,将""
更改为",,"