1. 概要

このチュートリアルでは、Linuxコマンドラインで2つのファイルを単語ごとに比較する方法を学習します。 Linuxには、2つのファイルを比較するコマンドdiffがすでにあります。 ただし、それらを1行ずつ比較し、それらの行内の単語を比較することはできません。

ここでは、2つのファイル間の単語の違いを表示する別のコマンドwdiffを使用します。

2. wdiffを使用する

wdiff はLinuxにプリインストールされていないため、次のようにインストールする必要があります。

$ sudo apt install wdiff

インストールが完了したら、次のことを確認する必要があります。

$ wdiff --version
wdiff (GNU wdiff) 1.2.2

Copyright (C) 1992, 1997, 1998, 1999, 2009, 2010, 2011, 2012 Free Software
Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Written by Franc,ois Pinard <[email protected]>.

ここで、first.txtとsecond.txtの2つのテキストファイルがあると仮定します。 cat を使用して、コンテンツを出力できます。

$ cat first.txt	
the quick brown fox jumps over the lazy dog
the woman snores and her husband wakes up
the sky is blue
$ cat second.txt
The quick yellow fox jumps over the sleeping dog
The man snores and his wife wakes up
It's raining

次に、 wdiff を実行して、違いを確認します。

$ wdiff first.txt second.txt
[-the-]{+The+} quick [-brown-] {+yellow+} fox jumps over the [-lazy-] {+sleeping+} dog
[-the woman-]
{+The man+} snores and [-her husband-] {+his wife+} wakes up
[-the sky is blue-]
{+It's raining+}

wdiffは、最初のファイルで変更する必要のある単語を示しているため、2番目のファイルと一致します。 角かっこ「[-word-]」は最初のファイルから削除する必要のある単語を示し、中かっこ「{++}」は最初のファイルに追加する必要のある単語を示します。

3. 出力のフィルタリング

オプション-1を追加すると、出力には、最初のファイルからを削除するために必要な単語が含まれなくなります。

$ wdiff -1 first.txt second.txt
{+The+} quick {+yellow+} fox jumps over the {+sleeping+} dog
{+The man+} snores and {+his wife+} wakes up
{+It's raining+}

現在、出力には角かっこが含まれていないことに注意してください。

-2 を追加すると、出力には含まれません最初のファイルに追加する必要のある単語が含まれます。

$ wdiff -2 first.txt second.txt
[-the-] quick [-brown-] fox jumps over the [-lazy-] dog
[-the woman-] snores and [-her husband-] wakes up
[-the sky is blue-]

最後に、 -3、を追加すると、出力には2つのファイル間で共通する単語が含まれなくなります。

$ wdiff -3 first.txt second.txt

======================================================================
[-the-]{+The+}
======================================================================
 [-brown-] {+yellow+}
======================================================================
 [-lazy-] {+sleeping+}
======================================================================

[-the woman-]
{+The man+}
======================================================================
 [-her husband-] {+his wife+}
======================================================================

[-the sky is blue-]
{+It's raining+}
======================================================================

4. ケースを無視する

wdiffはデフォルトで大文字と小文字を区別します。 大文字と小文字を区別しないように、オプション –ignore-caseを追加できます。

$ wdiff --ignore-case first.txt second.txt
The quick [-brown-] {+yellow+} fox jumps over the [-lazy-] {+sleeping+} dog
The [-woman-] {+man+} snores and [-her husband-] {+his wife+} wakes up
[-the sky is blue-]
{+It's raining+}

出力には、大文字と小文字が異なる同じ単語が含まれていないことがわかります。

5. 出力の色付け

wdiffの出力を読み取ることは、平均的なユーザーにとって難しい場合があります。 ただし、 wdiff の出力を色付けして理解しやすくする、colordiffというパッケージがあります。 Linuxにはデフォルトでcolordiffがありません。 したがって、それをインストールする必要があります。

$ sudo apt install colordiff

インストールが完了したら、wdiffの出力をcolordiffにパイプできます。

赤で印刷されている単語は最初のファイルから削除する必要がある単語であり、緑で印刷されている単語は最初のファイルに追加する必要がある単語です。

6. 結論

要約すると、 wdiff を使用すると、2つのファイル間の単語の違いを確認できます。 出力をフィルタリングするためのさまざまなオプションを追加できます。また、wdiffの出力を別のコマンドcolordiffにパイプして、読みやすく理解しやすくすることもできます。