nirasan's tech blog

趣味や仕事の覚え書きです。Linux, Perl, PHP, Ruby, Javascript, Android, Cocos2d-x, Unity などに興味があります。

git-diffでCSVファイルの変更を見やすくする

git-diffでは通常は行単位の差分しか表示されないので、CSVファイルの特定の行の1列だけ変更された場合にも、どの列が変更されたのか確認するのが難しい。

$ git diff
diff --git a/1.csv b/1.csv
index 6cb4d44..3d84fd8 100644
--- a/1.csv
+++ b/1.csv
@@ -1,2 +1,2 @@
-a, b, c
+a, d, c
 1, 2, 3

そこでword-diffオプションを指定すると単語単位での変更を指定してくれるようになる。

$ git diff --word-diff
diff --git a/1.csv b/1.csv
index 6cb4d44..3d84fd8 100644
--- a/1.csv
+++ b/1.csv
@@ -1,2 +1,2 @@
a, [-b,-]{+d,+} c
1, 2, 3

また、word-diffオプションは空白以外の連続を単語と判定しているので、新たな区切り文字を指定したい場合にはword-diff-regexオプションを使用すればよい。
以下は空白とカンマを区切り文字として指定する場合。

$ git diff --word-diff-regex="[^[:blank:],]"
diff --git a/2.csv b/2.csv
index bfde6bf..4f3e228 100644
--- a/2.csv
+++ b/2.csv
@@ -1,2 +1,2 @@
a,b,[-c-]{+z+}
1,[-2-]{+100+},3

以上