diff-输出行号


25

我想使用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吗?如果可以,怎么办?

Answers:


45

对的,这是可能的。使用这些选项时,默认设置是仅打印出每一行。这很冗长,不是您想要的。

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"

如果您要打印旧行而不是新行,请交换它们。


太好了!谢谢:)我试图从帮助列表中猜测,但是没有成功,并且以为我读错了。然后,我做了自己想要的Python(difflib清单对象中每行带有索引文件的模块),这是我无法使用的diff <(pipe buffer1) <(pipe buffer2),就像我计划的那样diff。现在,您救了我:)
zetah 2012年

如果您使用*且有目录,则无法使用。
Herman Toothrot
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.