What does an exclamation mark mean in diff output?

  • diff -u

may be what you need for your assignment.

To take your example and using diff -u

michael@x071:[/home/michael]diff -u file?
--- file1       2016-11-24 07:48:41 +0000
+++ file2       2016-11-24 07:48:57 +0000
@@ -1,3 +1,3 @@
 orange
 apple
-peach
+strawberry

A word of advice - RTM - or - Read The Manual. There are often other options. FYI: the historic options of diff (and diff3 when comparing three files) were to assist with creating "program inout" that would change file1 into file2 (or file2 back into file1). This has been the base of all "version control" software.

The diff options I remember from long ago:

  • -e : Produces output in a form suitable for use with the ed editor to convert File1 to File2.
  • -f : Produces output in a form not suitable for use with the ed editor, showing the modifications necessary to convert File1 to File2 in the reverse order of that produced under the -e flag.
  • -n : Produces output similar to that of the -e flag, but in the opposite order and with a count of changed lines on each insert or delete command. This is the form used by the revision control system (RCS).

The last option I will highlight is a "new" one - relatively speaking. (also several years old but was often not in POSIX implementations). Rather than creating output suitable for 'ed' of 'RCS' this is suitable for 'patch'

  • -u : Produces a diff command comparison with three lines of unified context. The output is similar to that of the -c flag, except that the context lines are not repeated; instead, the context, deleted, and added lines are shown together, interleaved.

IMHO: the key value of diff -c is as an improvement over the command 'cmp' - when you want to know more than ONLY if two files differ, or not. I had never paid attention (maybe it is a "new" option as well) - but shall think about it when my question is a recursive search for files that differ between two directory trees.


Your question is answered in diff's Info file, node Detailed Context:

The lines of context around the lines that differ start with two space characters. The lines that differ between the two files start with one of the following indicator characters, followed by a space character:

  • !

    A line that is part of a group of one or more lines that changed between the two files. There is a corresponding group of lines marked with ! in the part of this hunk for the other file.

  • +

    An "inserted" line in the second file that corresponds to nothing in the first file.

  • -

    A "deleted" line in the first file that corresponds to nothing in the second file.

The Info file has plenty of information about output formats, including header lines. I recommend you read through it again.


The output of diff is formed of chunks, each chunk corresponding to a set of changes. The *************** line marks the start of such a chunk.

Each chunk gives you the context in the files. *** 1,3 **** means that what follows are line 1 to 3 in the first file, while --- 1,3 ---- means that what follows are line 1 to 3 in the second file.

A minus sign - in the first column denotes lines that have been deleted, and a plus sign + marks lines that have been added. An exclamation point marks lines that have changed.

If your case, peach in the first file has been changed to strawberry in the second.

Tags:

Diff