如何使用 CSV 命令行工具在 CSV 中 "Partial-Transpose-and-Duplicate" 行

How to "Partial-Transpose-and-Duplicate" rows in a CSV with a CSV command line tool

我有一次又一次这样的 CSV 文件(格式为 table):

| PRODUCT | REGION   | COMMENT  | SALES1   | SALES2  | SALES3 |
| ------- | -------- | -------- | -------- | ------- | ------ |
| Orange  | Valencia | good     | Peter    |         |        |
| Apple   | Bavaria  | mediocre | Maria    | Walter  |        |
| Dates   | Dubai    | good     | Angelica | William | Jessie |

在此 CSV 中,我想将最后三列转置为一列,同时复制其他列的信息,这样结果将如下所示:

| PRODUCT | REGION   | COMMENT  | SALES    |
| ------- | -------- | -------- | -------- |
| Orange  | Valencia | good     | Peter    |
| Apple   | Bavaria  | mediocre | Maria    |
| Apple   | Bavaria  | mediocre | Walter   |
| Dates   | Dubai    | good     | Angelica |
| Dates   | Dubai    | good     | William  |
| Dates   | Dubai    | good     | Jessie   |

当然我可以写一点(Python)脚本来做到这一点,但我的问题是:

这是 tables/CSVs/matrices 上的标准操作吗?任何现有的 CSV 命令行工具都可以吗(如 csvkitmiller , csvtool, 等) 这可能开箱即用吗?

米勒 (https://github.com/johnkerl/miller) 从

开始
PRODUCT,REGION,COMMENT,SALES1,SALES2,SALES3
Orange,Valencia,good,Peter,,
Apple,Bavaria,mediocre,Maria,Walter,
Dates,Dubai,good,Angelica,William,Jessie

和运行

mlr --csv reshape -r "SALE" -o item,value then filter '$value=~".+"' then cut -x -f item then rename value,SALES input.txt

你将拥有

+---------+----------+----------+----------+
| PRODUCT | REGION   | COMMENT  | SALES    |
+---------+----------+----------+----------+
| Orange  | Valencia | good     | Peter    |
| Apple   | Bavaria  | mediocre | Maria    |
| Apple   | Bavaria  | mediocre | Walter   |
| Dates   | Dubai    | good     | Angelica |
| Dates   | Dubai    | good     | William  |
| Dates   | Dubai    | good     | Jessie   |
+---------+----------+----------+----------+