1) 根据另一个文件重新排序一个 csv 文件 header 和 2) 将一个 csv 文件的一列合并到另一个文件并删除重复项

1) Reordering one csv file based on another file header and 2) Merging one column of one csv file to another and remove duplicate

我有两个csv文件。 两个文件可能具有相同或不同的数据。文件 2 只有文件 1 中的几列。文件 2 中的某些列可能有不同的 header。例如,文件 2 的名字代替了名字

Username, Identifier,One-time password,Recovery code,First name,Last name,Department,Location
booker12,9012,12se74,rb9012,Rachel,Booker,Sales,Manchester
grey07,2070,04ap67,lg2070,Laura,Grey,Depot,London
johnson81,4081,30no86,cj4081,Craig,Johnson,Depot,London
jenkins46,9346,14ju73,mj9346,Mary,Jenkins,Engineering,Manchester
smith79,5079,09ja61,js5079,Jamie,Smith,Engineering,Manchester

文件 2

Department,First name,Last name,One-time password
Sales,Rachel,Booker,12se74
Depot,Laura,Grey,04ap67
Depot,Craig,Johnson,30no86
Engineering,Mary,Jenkins,14ju73
Engineering,Jamie,Smith,09ja61

问题一: 我想根据 headers.

对 csv 文件 2 中的列重新排序以匹配 csv 文件 1 中的顺序

所需输出 header 根据文件 1

排序
One-time password,Name,Last name,Department
12se74,Dash,Bok,Sales
04ap67,Claire,Trans,Accounts
30no86,Shane,Walter,Depot
14ju73,Leon,Jenkins,Engineering
09ja61,Oliver,Den,Engineering

问题二: 根据 header 删除重复项,将 File2 列合并到 File 1 例如,如果 First Name 、 Last Name 、 Department 列相同,那么这些是重复的,删除那些重复的。 其他列可能相同也可能不同。 因此想要实现基于条件的重复数据删除记录

问题三: 将文件 2 转换为文件 1 模板按顺序添加缺失的列。最后根据某些header比较并删除重复的列。 例如。如果名字、姓氏和密码相同,则它们是重复的,其他列可能相同或不同。

问题四: 将特定列从文件 2 复制到文件 1 保留顺序 例如。文件 2 有名称列 用文件 1 的名字列替换该列

尝试过:

awk -v FS=, -v OFS=, 'FNR==NR{hash[FNR]=; next}{ = hash[FNR]}1' file file2

以上回答来自https://unix.stackexchange.com/questions/674038/replace-a-column-value-in-csv-file-from-another-file

以上似乎可行。但它需要将列编号指定为 $5 和 $2。任何人都可以帮助修改上面的命令以指定 header 而不是列号

尝试过

awk -v FS=, -v OFS=, '{ for (i=1;i<=NF;i++) { if (i=="name") var=$i }; FNR==NR{hash[FNR]=; next}{$var = hash[FNR] }' file file2

不工作

您可以使用 Miller, which is available here 作为静态二进制文件轻松完成这一切。将 mlr 可执行文件放在您的 PATH 中的某个位置,您就完成了安装。


对于初学者,我假设我们正在处理两个 CSV 文件并且列名称没有不一致

cat file1.csv
Username, Identifier,One-time password,Recovery code,First name,Last name,Department,Location
booker12,9012,12se74,rb9012,Rachel,Booker,Sales,Manchester
cat file2.csv
Department,First name,Last name,One-time password
Engineering,Oliver,Den,09ja61
Sales,Rachel,Booker,12se74

重命名指定字段:
mlr --csv rename 'First name,Name' file2.csv
Department,Name,Last name,One-time password
Engineering,Oliver,Den,09ja61
Sales,Rachel,Booker,12se74

re-order file2.csv 的列基于 file1.csv 的 header:
mlr --csv reorder -f "$(head -n 1 file1.csv)" file2.csv
One-time password,First name,Last name,Department
09ja61,Oliver,Den,Engineering
12se74,Rachel,Booker,Sales

根据 file1.csv 的 header 添加 file2.csv 中缺少的列:
mlr --csv template -t file1.csv file2.csv
Username, Identifier,One-time password,Recovery code,First name,Last name,Department,Location
,,09ja61,,Oliver,Den,Engineering,
,,12se74,,Rachel,Booker,Sales,

根据 One-time password,First name,Last name 唯一性删除重复项:
mlr --csv head -n 1 -g 'One-time password,First name,Last name' fileX.csv

连接 file1.csvfile2.csv:
mlr --csv unsparsify file1.csv file2.csv
Username, Identifier,One-time password,Recovery code,First name,Last name,Department,Location
booker12,9012,12se74,rb9012,Rachel,Booker,Sales,Manchester
,,09ja61,,Oliver,Den,Engineering,
,,12se74,,Rachel,Booker,Sales,

连接 file1.csvfile2.csv 并根据 One-time password,First name,Last name 唯一性删除重复项:

命令由一系列操作组成。

mlr --csv unsparsify then head -n 1 -g 'One-time password,First name,Last name' file1.csv file2.csv
Username, Identifier,One-time password,Recovery code,First name,Last name,Department,Location
booker12,9012,12se74,rb9012,Rachel,Booker,Sales,Manchester
,,09ja61,,Oliver,Den,Engineering,

最后,假设列 First namefile2.csv 中被称为 Name 并且您想要连接 file1.csvfile2.csv 并根据 One-time password,First name,Last name unicity.

删除重复项

您可以通过在前一个命令前添加一个 rename 操作来实现:

mlr --csv rename 'Name,First name' then unsparsify then head -n 1 -g 'One-time password,First name,Last name' file1.csv file2.csv