- publishing free software manuals
Comparing and Merging Files with GNU diff and patch
by David MacKenzie, Paul Eggert, and Richard Stallman
Paperback (6"x9"), 120 pages
ISBN 0954161750
RRP £12.95 ($19.95)

"Well packaged... the quality of information is excellent" --- Linux User and Developer Magazine (Issue 36, Feb 2004) Get a printed copy>>>

2.6.2 Line Formats

Line formats control how each line taken from an input file is output as part of a line group in if-then-else format.

For example, the following command outputs text with a one-character change indicator to the left of the text. The first character of output is ‘-’ for deleted lines, ‘|’ for added lines, and a space for unchanged lines. The formats contain newline characters where newlines are desired on output.

diff \
   --old-line-format='-%l
' \
   --new-line-format='|%l
' \
   --unchanged-line-format=' %l
' \
   old new

To specify a line format, use one of the following options. You should quote format, since it often contains shell metacharacters.

--old-line-format=format
formats lines just from the first file.
--new-line-format=format
formats lines just from the second file.
--unchanged-line-format=format
formats lines common to both files.
--line-format=format
formats all lines; in effect, it sets all three above options simultaneously.

In a line format, ordinary characters represent themselves; conversion specifications start with ‘%’ and have one of the following forms.

‘%l’
stands for the contents of the line, not counting its trailing newline (if any). This format ignores whether the line is incomplete; See section 3 Incomplete Lines.
‘%L’
stands for the contents of the line, including its trailing newline (if any). If a line is incomplete, this format preserves its incompleteness.
‘%%’
stands for ‘%’.
‘%c'C'’
where C is a single character, stands for C. C may not be a backslash or an apostrophe. For example, ‘%c':'’ stands for a colon.
‘%c'\O'’
where O is a string of 1, 2, or 3 octal digits, stands for the character with octal code O. For example, ‘%c'\0'’ stands for a null character.
Fn’
where F is a printf conversion specification, stands for the line number formatted with F. For example, ‘%.5dn’ prints the line number using the printf format "%.5d". See section 2.6.1 Line Group Formats, for more about printf conversion specifications.

The default line format is ‘%l’ followed by a newline character.

If the input contains tab characters and it is important that they line up on output, you should ensure that ‘%l’ or ‘%L’ in a line format is just after a tab stop (e.g. by preceding ‘%l’ or ‘%L’ with a tab character), or you should use the -t or --expand-tabs option.

Taken together, the line and line group formats let you specify many different formats. For example, the following command uses a format similar to normal diff format. You can tailor this command to get fine control over diff output.

diff \
   --old-line-format='< %l
' \
   --new-line-format='> %l
' \
   --old-group-format='%df%(f=l?:,%dl)d%dE
%<' \
   --new-group-format='%dea%dF%(F=L?:,%dL)
%>' \
   --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)
%<---
%>' \
   --unchanged-group-format=" \
   old new
ISBN 0954161750Comparing and Merging Files with GNU diff and patchSee the print edition