29

I want to use cli tool for file comparison and need line-number before output line with which help I could jump to line difference, because I use tool which understands where to jump, if the line begins like this :line-number: regular line contents

So I tried diff, and reading documentation seems like it might be possible:

  -D, --ifdef=NAME                output merged file with `#ifdef NAME' diffs
      --GTYPE-group-format=GFMT   format GTYPE input groups with GFMT
      --line-format=LFMT          format all input lines with LFMT
      --LTYPE-line-format=LFMT    format LTYPE input lines with LFMT
    These format options provide fine-grained control over the output
      of diff, generalizing -D/--ifdef.
    LTYPE is `old', `new', or `unchanged'.  GTYPE is LTYPE or `changed'.
    GFMT (only) may contain:
      %<  lines from FILE1
      %>  lines from FILE2
      %=  lines common to FILE1 and FILE2
      %[-][WIDTH][.[PREC]]{doxX}LETTER  printf-style spec for LETTER
        LETTERs are as follows for new group, lower case for old group:
          F  first line number
          L  last line number
          N  number of lines = L-F+1
          E  F-1
          M  L+1
      %(A=B?T:E)  if A equals B then T else E
    LFMT (only) may contain:
      %L  contents of line
      %l  contents of line, excluding any trailing newline
      %[-][WIDTH][.[PREC]]{doxX}n  printf-style spec for input line number
    Both GFMT and LFMT may contain:
      %%  %
      %c'C'  the single character C
      %c'\OOO'  the character with octal code OOO
      C    the character C (other characters represent themselves)

but there is no example or explanation about this complicated switch.

Is it possible to get such output from diff? If so how?

2 Answers 2

55

Yes, it is possible. When using these options, the default is just to print out every line. This is very verbose, and not what you want.

diff --unchanged-line-format=""

will eliminate lines that are unchanged, so now only the old and new lines are produced.

diff --unchanged-line-format="" --new-line-format=":%dn: %L"

will now show the new lines prefixed by :<linenumber>: and a space, but still print the old lines. Assuming you want to eliminate them,

diff --unchanged-line-format="" --old-line-format="" --new-line-format=":%dn: %L"

If you want the old lines rather than the new ones to be printed, swap them around.

3
  • Just great! Thanks :) I tried to guess from help listing, but w/o success, and thought I misread it. Then I made what I wanted with Python (difflib module with indexed files per line in list object) only that I couldn't use diff <(pipe buffer1) <(pipe buffer2) with Python, like I planed with diff. Now you saved me :)
    – zetah
    Mar 24, 2012 at 2:59
  • it doesn't work if you use * and there are directories. Sep 7, 2016 at 7:41
  • Is it possible to use these options to format side-by-side diffs? Oct 28, 2020 at 3:19
0

Sometimes a picture or example is worth 1,000 words. I formed the following pipeline to "compare" two MySQL (structure) dumps based on the wnoise answer above (please give the any up-votes to wnoise).

Example side-by-side line numbers:

 diff   --unchanged-line-format="" --old-line-format="%dn: %L  " --new-line-format="| %dn: %L"  \
         ./20220202-msqldump.sql  ./20221130-msqldump.sql       |
     awk -e' /^[[:digit:]]+: )/{ previous = $0; next; } { print previous $0 }' |
     grep -v -e"ENGINE=InnoDB AUTO_INCREMENT="

Sadly I discovered that the diff option to do: --side-by-side is not supported with the line-format options.

Eliminating the AUTO_INCREMENT lines left me with just two differences being the date and so on.

Without the grep filter the output looks like:

5127: ) ENGINE=InnoDB AUTO_INCREMENT=340 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; | 5105: ) ENGINE=InnoDB AUTO_INCREMENT=271 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 5150: ) ENGINE=InnoDB AUTO_INCREMENT=895 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; | 5128: ) ENGINE=InnoDB AUTO_INCREMENT=763 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 5170: ) ENGINE=InnoDB AUTO_INCREMENT=1371 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; | 5148: ) ENGINE=InnoDB AUTO_INCREMENT=1173 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

Notice that the line numbers are not matching. I used meld to verify that things were lining up first.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .