【问题标题】:Compare two csv files containing similar columns, eliminate duplicate and output unique content in one file比较两个包含相似列的 csv 文件,消除重复并在一个文件中输出唯一内容
【发布时间】:2016-10-18 13:40:45
【问题描述】:

我有两个 csv 文件(格式相似)

file1.csv

 post_status    post_type   post_content    post_title
 publish        post        paragraph one   title one
 publish        post        paragraph two   title two
 publish        post        paragraph three title three
 publish        post        paragraph four  title four

file2.csv

 post_status    post_type   post_content    post_title
 publish        post        paragraph one   title one
 publish        post        paragraph two   title two
 publish        post        paragraph three title three
 publish        post        paragraph four  title four
 publish        post        paragraph five  title five
 publish        post        paragraph six   title six

Desired-output.csv

 post_status    post_type   post_content    post_title
 publish        post        paragraph five  title five
 publish        post        paragraph six   title six

到目前为止我得到的解决方案是使用 Power Shell 和这个:

cat first.csv second.csv | sort -u >result.csv

这会产生无法保持原始文件一致性的结果。

我正在使用 Ubuntu 和 Windows。寻找一个简单优雅的解决方案。我们将不胜感激。

【问题讨论】:

  • @whatever 谢谢。我会尝试哪个平台? Windows cmd 还是 Ubuntu 的终端?

标签: windows powershell ubuntu compare


【解决方案1】:

试试

$Content = Get-Content first.csv,second.csv
$Content | Select-Object -Unique | Out-File result.csv

此解决方案适用于 Windows Powershell

实际上,你可以在一行中完成所有操作:

Get-Content first.csv,second.csv | Select-Object -Unique | Out-File result.csv

您的版本和我的版本之间的唯一区别(除了使用 linux 别名作为 powershell cmds 之外)是您使用的是 Sort-Object,它将更改条目的顺序。:)

【讨论】:

    猜你喜欢
    • 2013-06-24
    • 2016-08-16
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2018-03-14
    • 1970-01-01
    相关资源
    最近更新 更多