2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

順番がバラバラの2つのCSVファイルを比較する

Posted at

順番が入れ替わってしまっている2つのCSVファイルを比較して、実質一致しているかどうか、違うならどこが違うかを手軽に知る方法です。

比較する方法

PowerShellを使います。

以下のいずれかを実行します。どちらも全く同じです。

Compare-Object (Get-Content a.csv) (Get-Content a.csv)
# または
diff (cat a.csv) (cat b.csv)

順番を無視すれば一致している場合は、何も表示されません。

不一致がある場合は、その行の内容が表示されます。

CSVファイルの内容が以下であるとします。

a.csv
id,name
1,sato
2,suzuki
3,takahashi
4,tanaka
b.csv
id,name
2,suzuki
3,takahashi
1,sato

上述したコマンドで比較すると、以下の結果が表示されます。

InputObject SideIndicator
----------- -------------
4,tanaka    <=

これは、左のファイルにだけ 4,tanaka という行があることを示しています。

備考

Get-Content の代わりに Import-Csv でもよいです。

PS > diff (import-csv a.csv) (import-csv b.csv)

InputObject          SideIndicator
-----------          -------------
@{id=4; name=tanaka} <=
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?