0

I have the following command:

diff -u filea fileb | grep '^-' | sed 's/^-//' > diff.txt

It works great; it outputs a list of differences per line to a text file, and then removes every instance of '-' against each line, however I really want to understand what -u is doing and what the following means:

Output NUM (default 2) lines of unified context

Am I right in thinking unified refers to only displaying the differences rather than the context of the differences?

Also are there any potential gotchas I should be concerned about with regard to my requirement of displaying differences between two files to another file using the above command?

Imran Azad
  • 1,008
  • 2
  • 12
  • 30

1 Answers1

0

This stands for the amount of "context lines" around each diff it finds.

Probably best demonstrated with an example:

$ diff -U 1 hello.txt hello2.txt
--- hello.txt   2015-01-24 16:10:45.000000000 +0100
+++ hello2.txt  2015-01-24 16:11:14.000000000 +0100
@@ -4,3 +4,3 @@
 Hello World.
-Hello World.
+Yo World.
 Hello World.

$ diff -U 2 hello.txt hello2.txt
--- hello.txt   2015-01-24 16:10:45.000000000 +0100
+++ hello2.txt  2015-01-24 16:11:14.000000000 +0100
@@ -3,5 +3,5 @@
 Hello World.
 Hello World.
-Hello World.
+Yo World.
 Hello World.
 Hello World.

$ diff -U 3 hello.txt hello2.txt
--- hello.txt   2015-01-24 16:10:45.000000000 +0100
+++ hello2.txt  2015-01-24 16:11:14.000000000 +0100
@@ -2,7 +2,7 @@
 Hello World.
 Hello World.
 Hello World.
-Hello World.
+Yo World.
 Hello World.
 Hello World.
 Hello World.
mcls
  • 9,071
  • 2
  • 29
  • 28