update

Home

1 Cheat on Unified Diff command (diff -u)

Unified diff gives you slightly different syntax than just a straight diff. I ususally stick to the straight diff command, but -u gives more precise line number references. Also, git uses the unfied diff output in git diff commands

1.1 unified diff output meaning

When run in unified diff mode with the -u option you get:

First two lines are

--- from-file from-file-modification-time
+++++ to-file to-file-modification-time
--- from-file    from-file-modification-time
+++ to-file        to-file-modification-time

Next come one or more chunks of differences with this syntax:

@@ from-file-line-numbers to-file-line-numbers @@
line-from-either-file
line-from-either-file

If a chunk and its context contain two or more lines, its line numbers look like ‘start,count’. Otherwise only its end line number appears.

The lines common to both files begin with a space character.

The lines that actually differ between the two files have either a + or a - characters in the left print column:

‘+’
A line was added here to the first file.

‘-’
A line was removed here from the first file.

Here is a larger example:

$ diff -u 1stfile 2ndfile
--- 1stfile 2020-05-22 11:32:48.000000000 -0400
+++ 2ndfile 2021-02-02 16:20:15.000000000 -0500
@@ -1,12 +1,10 @@
 # The first file has twelve lines in this block
 # The second file ends up with only 10 lines in this block
 #
-# macOS Notice
+# macOS Notice  This line was changed on the 2nd file.
 # 
 # This file is not consulted for DNS hostname resolution, address
 # resolution, or the DNS query routing.
 #
-# This line is in the first file, but will be removed in 2nd file
-# This line also will be removed in 2nd file.
 #
@@ -17,3 +15,5 @@
 nameserver 208.67.220.220
 You can get so confused that you'll start in to race
 down long wiggled roads at a break-necking pace
+#  And        # Two new lines added to second file,
+# On and on . # hence now we are looking at a block of 5 lines in 2nd.

1.2 Home