diff output understanding

GNU diff is a fantastic tool to compare files or even directories line by line which can be found in your command line out of box.

Normally, we could learn its usage from man diff. But this is just a basic manual lacking of detailed explanation. The full documentation for diff is maintained as a info manual. If the info and diff programs are properly installed at your site, the command info diff should give you access to the complete manual.

But info is rather rare now, so we might need to learn how to use info.

After learning the how to use info and read the diff info manual, let’s analysis the example in compare files or even directories.

If we diff two files such as diff <file1> <file2>, then, < denotes lines in <file1>, > denotes lines in <file2>.

But what are the meanings of these characters: 5d4, 9,23c8, 4,33d3, 5a6 etc..

The following is the format description in diff.info:

Detailed Description of Normal Format
-------------------------------------

   The normal output format consists of one or more hunks of
differences; each hunk shows one area where the files differ.  Normal
format hunks look like this:

     CHANGE-COMMAND
     < FROM-FILE-LINE
     < FROM-FILE-LINE...
     ---
     > TO-FILE-LINE
     > TO-FILE-LINE...

   There are three types of change commands.  Each consists of a line
number or comma-separated range of lines in the first file, a single
character indicating the kind of change to make, and a line number or
comma-separated range of lines in the second file.  All line numbers are
the original line numbers in each file.  The types of change commands
are:

`LaR'
     Add the lines in range R of the second file after line L of the
     first file.  For example, `8a12,15' means append lines 12-15 of
     file 2 after line 8 of file 1; or, if changing file 2 into file 1,
     delete lines 12-15 of file 2.

`FcT'
     Replace the lines in range F of the first file with lines in range
     T of the second file.  This is like a combined add and delete, but
     more compact.  For example, `5,7c8,10' means change lines 5-7 of
     file 1 to read as lines 8-10 of file 2; or, if changing file 2 into
     file 1, change lines 8-10 of file 2 to read as lines 5-7 of file 1.

`RdL'
     Delete the lines in range R from the first file; line L is where
     they would have appeared in the second file had they not been
     deleted.  For example, `5,7d3' means delete lines 5-7 of file 1;
     or, if changing file 2 into file 1, append lines 5-7 of file 1
     after line 3 of file 2.

So,

  • 5d4: line 5 of <file1> was deleted, line 4 is where it would have appeared in <file2> if it was not deleted. This means that the original line 6 of <file1> is line 5(4+1) of <file2> which looks like original line 5 of <file1> is at line 4 in <file2>(Just image!). The comparasion picture as below:

DevTools

  • 9,23c8: change lines 9-23 of <file1> to read as line 8 of <file2>.
  • 4,33d3: line 4-33 of <file1> were deleted, line 3 is where it would have appeared in <file2> if they were not deleted as next common line is 4 in <file2>
  • 5a6: Add line 6 of <file2> after line 5 of <file1>. The comparasion picture as below:

DevTools

References

unix.stackexchange.com/questions/8...

Further Reading

Basic “info” tutorial in stand-alone program | 开发者工具论坛

本作品采用《CC 协议》,转载必须注明作者和本文链接
日拱一卒
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
93
粉丝
85
喜欢
153
收藏
121
排名:71
访问:11.4 万
私信
所有博文
社区赞助商