Diff

diff - 輸出行號

  • December 8, 2013

我想使用 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?如果有怎麼辦?

對的,這是可能的。使用這些選項時,預設設置只是列印出每一行。這是非常冗長的,而不是你想要的。

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"

如果您希望列印舊行而不是新行,請交換它們。

引用自:https://unix.stackexchange.com/questions/34874