![如何使用 awk 使用键列连接两个 csv 并写入另一个 csv](https://linux22.com/image/12979/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20awk%20%E4%BD%BF%E7%94%A8%E9%94%AE%E5%88%97%E8%BF%9E%E6%8E%A5%E4%B8%A4%E4%B8%AA%20csv%20%E5%B9%B6%E5%86%99%E5%85%A5%E5%8F%A6%E4%B8%80%E4%B8%AA%20csv.png)
我有两个包含以下数据的 CSV,
文件1.csv:-
path(S1),filename,timestamp(S1),memory(S1)
xx/APS50CC2,APS50CC2,23-09-2019,45
yy/APS50CC3,APS50CC3,18-09-2019,25
文件2.csv:-
path(S2),filename,timestamp(S2),memory(S2)
mm/APS03CR1,APS03CR1,23-09-2019,200
zz/APS50CC2,APS50CC2,18-09-2019,55
预期输出
output.csv:-
filename,path(S1),timestamp(S1),memory(S1),path(S2),timestamp(S2),memory(S2)
APS50CC2,xx/APS50CC2,23-09-2019,45,zz/APS50CC2,18-09-2019,55
APS50CC3,yy/APS50CC3,18-09-2019,25,Not found,Not found,Not found
APS03CR1,Not found,Not found,Not found,mm/APS03CR1,23-09-2019,200
我已经尝试过join
命令并运行了,它对于较小的输入集运行良好。但是当我运行大约 500000+ 条记录时,它会给出错误,因此我不想使用join
,我想尝试使用awk
...
我对 有点陌生awk
,请帮助我解决语法问题。这里加入的关键是第 2 列,即filename
。
答案1
使用米勒(https://github.com/johnkerl/miller)并运行
mlr --csv join --ul --ur -j filename -f file1.csv \
then unsparsify --fill-with "Not found" \
then reorder -f "filename","path(S1)","timestamp(S1)","memory(S1)","path(S2)","timestamp(S2)","memory(S2)" file2.csv
你将会拥有
+----------+-------------+---------------+-------------+---------------+------------+--------------+
| filename | path(S1) | timestamp(S1) | path(S2) | timestamp(S2) | memory(S2) | memory(S1) |
+----------+-------------+---------------+-------------+---------------+------------+--------------+
| APS03CR1 | Not found | Not found | mm/APS03CR1 | 23-09-2019 | 200 | Not found |
| APS50CC2 | xx/APS50CC2 | 23-09-2019 | zz/APS50CC2 | 18-09-2019 | 55 | 45 |
| APS50CC3 | yy/APS50CC3 | 18-09-2019 | Not found | Not found | Not found | 25 |
+----------+-------------+---------------+-------------+---------------+------------+--------------+
我知道它不是 awk,但 Miller 是另一个很棒的结构化文本工具