使用 UNIX shell 进行 vlookup

use*_*638 -3 linux csv

我有这个 CSV 文件

ID,col1,col2,col3
1,,,FALSE
2,,,FALSE
3,,,FALSE
4,,,FALSE
Run Code Online (Sandbox Code Playgroud)

我想使用 ID 列将其与另一个 CSV 文件结合起来

ID,col1
2,AA
3,BB
Run Code Online (Sandbox Code Playgroud)

获取(真正的输出必须是另一个CSV,这里有一个漂亮的打印输出,以便更具可读性)

+----+------+------+-------+
| ID | col1 | col2 | col3  |
+----+------+------+-------+
| 1  | -    | -    | FALSE |
| 2  | AA   | -    | FALSE |
| 3  | BB   | -    | FALSE |
| 4  | -    | -    | FALSE |
+----+------+------+-------+
Run Code Online (Sandbox Code Playgroud)

最后当col1不为空时,col3必须是TRUE

+----+------+------+-------+
| ID | col1 | col2 | col3  |
+----+------+------+-------+
| 1  | -    | -    | FALSE |
| 2  | AA   | -    | TRUE  |
| 3  | BB   | -    | TRUE  |
| 4  | -    | -    | FALSE |
+----+------+------+-------+
Run Code Online (Sandbox Code Playgroud)

如何使用一些 Linux 命令行实用程序或 bash 脚本来做到这一点?

abo*_*uso 5

您可以使用米勒(https://github.com/johnkerl/miller)。跑步

mlr --csv join --ul -j ID -f input01.csv then put -S '!is_empty($col1) { $col3 = "TRUE" }' then sort -n ID input02.csv >output.csv
Run Code Online (Sandbox Code Playgroud)

你将会拥有

+----+------+------+-------+
| ID | col1 | col2 | col3  |
+----+------+------+-------+
| 1  | -    | -    | FALSE |
| 2  | AA   | -    | TRUE  |
| 3  | BB   | -    | TRUE  |
| 4  | -    | -    | FALSE |
+----+------+------+-------+
Run Code Online (Sandbox Code Playgroud)

一些注意事项:

  • join使用字段运行连接ID
  • put检查 col1 是否不为空;
  • sort按 ID 对记录排序