我想使用 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)
Run Code Online (Sandbox Code Playgroud)
但是没有关于这个复杂开关的例子或解释。
是否有可能从 获得这样的输出diff
?如果是这样怎么办?
wno*_*ise 49
对的,这是可能的。使用这些选项时,默认值只是打印出每一行。这是非常冗长的,而不是你想要的。
diff --unchanged-line-format=""
Run Code Online (Sandbox Code Playgroud)
将消除未更改的行,因此现在只生成旧行和新行。
diff --unchanged-line-format="" --new-line-format=":%dn: %L"
Run Code Online (Sandbox Code Playgroud)
现在将显示以:<linenumber>:
空格为前缀的新行,但仍打印旧行。假设你想消除它们,
diff --unchanged-line-format="" --old-line-format="" --new-line-format=":%dn: %L"
Run Code Online (Sandbox Code Playgroud)
如果您希望打印旧行而不是新行,请交换它们。