web-dev-qa-db-ja.com

単純なdiffコマンド出力

Diffコマンドの使用例は数多く見ましたが、基本的な使用法の詳細はありません。使用したい2つのファイルの内容は次のとおりです。

cat -A file1.txt
a$
b$
c$
d$

cat -A file2.txt
b$
c$
d$
e$

Diffコマンドを次のように使用すると:

diff file1.txt file2.txt

私は得る:

1d0
< a
4a4
> e

私が知りたいのは、小なり記号と大なり記号に加えて、1d0の行1と0、および4a4の行4と4が何を意味するかということです。より一般的には、大なり記号とは対照的に、aの前に小なり記号があります。違いは何ですか?

6

diffマニュアル から:

lar}
最初のファイルの行lの後に、2番目のファイルの範囲rの行を追加します。たとえば、「8a12,15」は、ファイル1の行8の後にファイル2の行12〜15を追加することを意味します。または、ファイル2をファイル1に変更する場合は、ファイル2の行12〜15を削除します。

fct}
最初のファイルの範囲fの行を2番目のファイルの範囲tの行に置き換えます。これは、追加と削除を組み合わせたようなものですが、よりコンパクトです。たとえば、「5,7c8,10」は、ファイル1の5〜7行目を変更して、ファイル2の8〜10行目として読み取ることを意味します。または、ファイル2をファイル1に変更する場合、ファイル2の行8〜10を変更して、ファイル1の行5〜7として読み取ります。

rdl}
最初のファイルから範囲rの行を削除します。 linelは、削除されなかった場合に2番目のファイルに現れる場所です。たとえば、「5,7d3」は、ファイル1の5〜7行目を削除することを意味します。または、ファイル2をファイル1に変更する場合、ファイル2の行3の後にファイル1の行5〜7を追加します。

>および<は、 サイドバイサイド出力形式 を見ると意味があります。

<
ファイルは異なり、最初のファイルのみに行が含まれます。

>
ファイルは異なり、2番目のファイルのみに行が含まれます。

マニュアルの出力例:

  • 並んで:

    The Way that can be told of is n   <
    The name that can be named is no   <
    The Nameless is the Origin of He        The Nameless is the Origin of He
    The Named is the mother of all t   |    The named is the mother of all t
                                       >
    Therefore let there always be no        Therefore let there always be no
      so we may see their subtlety,           so we may see their subtlety,
    And let there always be being,          And let there always be being,
      so we may see their outcome.            so we may see their outcome.
    The two are the same,                   The two are the same,
    But after they are produced,            But after they are produced,
      they have different names.              they have different names.
                                       >    They both may be called deep and
                                       >    Deeper and more profound,
                                       >    The door of all subtleties!
    
  • 通常:

    1,2d0
    < The Way that can be told of is not the eternal Way;
    < The name that can be named is not the eternal name.
    4c2,3
    < The Named is the mother of all things.
    ---
    > The named is the mother of all things.
    > 
    11a11,13
    > They both may be called deep and profound.
    > Deeper and more profound,
    > The door of all subtleties!
    
11
muru

Diffコマンドについて詳しく説明します here 。ページ上部の「How diff Works」の下に探しているものがあります。

具体的には、1d0は、最初のファイルから1行削除して、行0まで同期する必要があることを意味します。これはファイルの最初の行ではなく、基本的に言っています。その削除を行うと、両方のファイルが空のポイントから開始されます。ファイル1の出力の次の行は、両方のファイルの出力の最初の行です(つまり、次の行は行1でなければなりません)。

読みやすいものが必要な場合は、diff -c file1.txt file2.txtを実行した方が良いかもしれません。

3
b_laoshi