7

Looks like -u can take an extra number argument, but I don't quiet get the manual,

It says,

  -u, -U NUM, --unified[=NUM]
          output NUM (default 3) lines of unified context

Someone name an example please?

I couldn't find a working one.

10

-u by itself outputs results in "unified" format the number just changes the number of neighboring lines included as context.

4

Directly from the Wikipedia article on diff:

Unified context diffs were originally developed by Wayne Davison in August 1990 (in unidiff which appeared in Volume 14 of comp.sources.misc). Richard Stallman added unified diff support to the GNU Project's diff utility one month later, and the feature debuted in GNU diff 1.15, released in January 1991. GNU diff has since generalized the context format to allow arbitrary formatting of diffs.

The format starts with the same two-line header as the context format, except that the original file is preceded by "---" and the new file is preceded by "+++". Following this are one or more change hunks that contain the line differences in the file. The unchanged, contextual lines are preceded by a space character, addition lines are preceded by a plus sign, and deletion lines are preceded by a minus sign.

A hunk begins with range information and is immediately followed with the line additions, line deletions, and any number of the contextual lines. The range information is surrounded by double-at signs, and combines onto a single line what appears on two lines in the context format (above). The format of the range information line is as follows:

@@ -l,s +l,s @@ optional section heading

Then throughout the file where there are changes to be made you'll lines like these:

-check this dokument. On
+check this document. On

NOTE: A - means it's being removed, and a + means it's getting added.

The command diff -U can also take an additional parameter, a number, that signifies how many lines of neighboring text around the point where the difference between the 2 files is occurring. This is helpful in getting better context of what the differences actually entail.

Example

file1

$ cat file1.txt 
The Rain in Spain by
Servants Poor Professor Higgins!
Poor Professor Higgins! Night and day
He slaves away! Oh, poor Professor Higgins!
All day long On his feet; Up and down until he's numb;
Doesn't rest; Doesn't eat;

file2

$ cat file2.txt 
The Rain in Spain by
added extra line here
Servants Poor Professor Higgins!
Poor Professor Higgins! Night and day
He slaves away! Oh, poor Professor Higgins!
All day long On his feat; Up and down untile he's numb;
Doesn't rest; Doesn't eat;

diff

$ diff -U 2 file1.txt file2.txt 
--- file1.txt   2013-04-26 09:39:13.496835363 -0400
+++ file2.txt   2013-04-26 09:38:04.838299195 -0400
@@ -1,6 +1,7 @@
 The Rain in Spain by
+added extra line here
 Servants Poor Professor Higgins!
 Poor Professor Higgins! Night and day
 He slaves away! Oh, poor Professor Higgins!
-All day long On his feet; Up and down until he's numb;
+All day long On his feat; Up and down untile he's numb;
 Doesn't rest; Doesn't eat;

See diff on wikipedia for more details.

  • 2
    I think the OP's main question wasn't with the diff utility itself but what was the use for the number beside -u in his version of diff. – Bratchley Apr 26 '13 at 13:23
  • Thanks @JoelDavis, I missed the word number in his question. I'll update to mention it my answer as well. – slm Apr 26 '13 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.