我有两个包含以下数据的 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 是另一个很棒的结构化文本工具