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"

如果您想要列印舊行而不是新行,請將它們交換。

答案2

有時一張圖片或一個例子抵得上 1000 個單字。我根據wnoise上面的答案形成了以下管道來「比較」兩個 MySQL(結構)轉儲(請給予任何贊成票wnoise)。

並排行號範例:

 diff   --unchanged-line-format="" --old-line-format="%dn: %L  " --new-line-format="| %dn: %L"  \
         ./20220202-msqldump.sql  ./20221130-msqldump.sql       |
     awk -e' /^[[:digit:]]+: )/{ previous = $0; next; } { print previous $0 }' |
     grep -v -e"ENGINE=InnoDB AUTO_INCREMENT="

遺憾的是,我發現行格式選項不支援diffdo: 選項。--side-by-side

消除 AUTO_INCRMENT 行只留下兩個區別,即日期等。

如果沒有grep過濾器,輸出看起來像:

第5127章|第5105章第5150章|第5128章第5170章|第5148章

請注意,行號不符。我過去常常meld先確認事情是否排列整齊。

相關內容