Imagine you have two versions of a text file and need to view the differences between them. The ‘diff’ command will show you the lines that have been added/deleted/changed, but let’s say you need to know which record each change is in. If the text files have a regular structure to them, you can use the wonderful ‘–show-function-line=’ option…

For example, I knew that a DDL file had been modified to add foreign key constraints, but I needed to know in which tables:

diff -U 0 --show-function-line='^CREATE ' affiliate.ddl.old affiliate.ddl
--- affiliate.ddl.old   2011-06-13 11:24:08.889206044 +0100
+++ affiliate.ddl       2011-06-13 11:48:54.281217101 +0100
@@ -80,2 +80,2 @@ CREATE TABLE `tblTrackedLinkTagMap` (
-  FOREIGN KEY (intTrackedLinkId) REFERENCES tblTrackedLink (intTrackedLinkId),
-  FOREIGN KEY (intTrackedLinkTagId) REFERENCES ublTrackedLinkTag (intTrackedLinkTagId)
+  FOREIGN KEY (intTrackedLinkId) REFERENCES tblTrackedLink (intTrackedLinkId) ON DELETE CASCADE,
+  FOREIGN KEY (intTrackedLinkTagId) REFERENCES ublTrackedLinkTag (intTrackedLinkTagId) ON DELETE CASCADE
@@ -94 +94 @@ CREATE TABLE `tblTrackedLinkTagValue` (
-  FOREIGN KEY (intTrackedLinkTagMapId) REFERENCES tblTrackedLinkTagMap (intTrackedLinkTagMapId)
+  FOREIGN KEY (intTrackedLinkTagMapId) REFERENCES tblTrackedLinkTagMap (intTrackedLinkTagMapId) ON DELETE CASCADE

In the above you can see the ‘CREATE TABLE’ info that I needed.

Another example would be seeing in which ‘div’ changes happened in an html file.

diff -U 0 --show-function-line='<div ' ...

Or in which routine changes happened in a perl file.

diff -U 0 --show-function-line='^sub ' ...