Csv

比較 2 個 csv 文件並僅將差異輸出到變數中

  • March 9, 2018

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 中的行發生更改,則沒有更改 print 已更改的行 + 行內容,如果文件 1.csv 中的行失去 print “已刪除的行 + 行內容,如果行添加了文件 1.csv 列印“添加了行”+ 行內容,到目前為止,我有這個,它列印所需的輸出,但是否可以檢測到添加/刪除的內容

     comm -1 -3 <(sort 1.csv) <(sort 2.csv)
citrix-xendesktop-ec2-provisioning",AmazonEC2FullAAA
citrix-xendesktop-ec2-provisioning",AmazonS3FullAccess
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq,qqqqqqqqqqqqqqqq

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

引用自:https://unix.stackexchange.com/questions/429310