![比较 2 个 csv 文件并仅将差异输出到变量中](https://linux22.com/image/129240/%E6%AF%94%E8%BE%83%202%20%E4%B8%AA%20csv%20%E6%96%87%E4%BB%B6%E5%B9%B6%E4%BB%85%E5%B0%86%E5%B7%AE%E5%BC%82%E8%BE%93%E5%87%BA%E5%88%B0%E5%8F%98%E9%87%8F%E4%B8%AD.png)
1.csv:
rundeck-read-only-iam-permissions,IAMReadOnlyAccess
citrix-xendesktop-ec2-provisioning",AmazonEC2FullAccess
2.csv:
citrix-xendesktop-ec2-provisioning",AmazonEC2FullAAA
citrix-xendesktop-ec2-provisioning",AmazonS3FullAccess
rundeck-read-only-iam-permissions,IAMReadOnlyAccess
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq,qqqqqqqqqqqqqqqq
注意顺序不一样
我想比较文件 2.csv 与文件 1.csv 中的差异(以及 2.csv 中的订单行以匹配 1.csv 中的订单)
如果没有更改,则打印: 如果文件 1.csv 中的行发生更改,则不会更改 打印已更改的行+行内容,如果文件 1.csv 中的行丢失,则打印“已删除的行 + 行内容,如果行在添加文件 1.csv 打印“已添加行”+行内容,到目前为止,我有这个,它打印所需的输出,但是否可以检测添加/删除的内容
comm -1 -3 <(sort 1.csv) <(sort 2.csv)
citrix-xendesktop-ec2-provisioning",AmazonEC2FullAAA
citrix-xendesktop-ec2-provisioning",AmazonS3FullAccess
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq,qqqqqqqqqqqqqqqq
答案1
diff
可能是您想要的工具。以下是三个示例文件:
$ paste foo bar baz
aaa aaa aaa
aaa aaa aaz
aaa aaa aaa
$ if diff <(sort foo) <(sort bar); then echo "No differences"; fi
No differences
$ if diff <(sort foo) <(sort baz); then echo "No differences"; fi
3c3
< aaa
---
> aaz