diff - 行番号出力

diff - 行番号出力

ファイル比較のためにcliツールを使用したいと思い、ラインを出力する前にライン番号が必要です。このツールを使用すると、行の違いに移動できます。なぜなら、私が使用するツールは、行がこのように始まるとジャンプする場所を理解できるからです。:line-number: regular line contents

だから私はそれを試してdiff、可能なように見える文書を読んだ。

  -D, --ifdef=NAME                output merged file with `#ifdef NAME' diffs
      --GTYPE-group-format=GFMT   format GTYPE input groups with GFMT
      --line-format=LFMT          format all input lines with LFMT
      --LTYPE-line-format=LFMT    format LTYPE input lines with LFMT
    These format options provide fine-grained control over the output
      of diff, generalizing -D/--ifdef.
    LTYPE is `old', `new', or `unchanged'.  GTYPE is LTYPE or `changed'.
    GFMT (only) may contain:
      %<  lines from FILE1
      %>  lines from FILE2
      %=  lines common to FILE1 and FILE2
      %[-][WIDTH][.[PREC]]{doxX}LETTER  printf-style spec for LETTER
        LETTERs are as follows for new group, lower case for old group:
          F  first line number
          L  last line number
          N  number of lines = L-F+1
          E  F-1
          M  L+1
      %(A=B?T:E)  if A equals B then T else E
    LFMT (only) may contain:
      %L  contents of line
      %l  contents of line, excluding any trailing newline
      %[-][WIDTH][.[PREC]]{doxX}n  printf-style spec for input line number
    Both GFMT and LFMT may contain:
      %%  %
      %c'C'  the single character C
      %c'\OOO'  the character with octal code OOO
      C    the character C (other characters represent themselves)

ただし、この複雑なスイッチの例や説明はありません。

そのような出力を得ることができますかdiff?それではどうでしょうか?

ベストアンサー1

はい、可能です。これらのオプションを使用する場合、デフォルトは各行を印刷することです。これは非常に冗長であり、望むものではありません。

diff --unchanged-line-format=""

変更されていない行は削除されるため、以前の行と新しい行のみが作成されます。

diff --unchanged-line-format="" --new-line-format=":%dn: %L"

:<linenumber>:これで、空白の前に付いた新しい行が表示されますが、前の行は印刷され続けます。それらを削除したいとしましょう。

diff --unchanged-line-format="" --old-line-format="" --new-line-format=":%dn: %L"

新しい行の代わりに既存の行を印刷するには、行を置き換えます。

おすすめ記事