When comparing two files, how can I get the exact line number of changed locations? - diff

I need to compare two source files and obtain the exact line number of changed statements. I used GNU diff and output in a unified format. However, this output only shows the changed chunk (identified by line ranges). But what I really want is some tool that can directly give me this:
(about the new file)
line 5: added;
line 11: modified
(about the old file)
line 7: deleted
Is there any tool that can achieve this? Or is there any option in GNU diff that can achieve this?
Thank you!

Graphical tools like meld or kdiff3 show both files with line numbers. Maybe it is what your are looking for.

Related

Two closely matching files: get corresponding lines?

I'm in a situation where I'm programmatically generating LaTeX code, and I want my Synctex to point to the correct lines in the original file.
The generation is basically doing template expansion, so the original files are nearly identical to the generated ones, but with some snippets expanded.
I'm wondering, is there a diff tool or library that will easily give me the line number of the original file that corresponds to a given line in the generated one? Can this be extracted from a normal Unix diff somehow?
This is part of a build script, so ideally something easy to run, like bash or python, is preferred to something that needs to be compiled.
Google’s diff-match-patch lib is a neat solution to questions like these: https://github.com/google/diff-match-patch

parse text file to find lines which contain a date after 05/05/2011

I could like to be able to list the line no.s in a file which contain a date, in the format dd,mm,yy, which is greater than 05/05/11
the file in question is prolog source code - the dates form part of a comment indicating when the modification after the comment was made
I'm using emacs, so thought an emacs solution would be the obvious way forward but willing to think more laterally if need be
a typical line of interest would be:
4.00 21/10/12 Modified to incorporate proportional match tolerances
I would like to report the file and line no. e.g.
filename line no.
my_source_1.pl 37
or alternatively, just being able to step through the file using Regexp I-search highlighting each dd/mm/yy that is greater than 05/05/2011 would be very useful

diff ignore certain pattern in the file

I want to make diff between two files which contains lines beginning with "line_$NR". I want to make diff between the files making abstraction of the presence of "lines_$NR" but when the differences are printed I want lines_$NR to be displayed.
It is possible to do that?
Thanks.
I believe in this case, you have to preprocess your iput files to remove /^line_[0-9]*/, diff the resulting files, then recombine the diff output with the removed words according to line numbers in diff output.
Python's difflib should be very handy here, or same from perl. If you want to stick to shell, I suppose you could get by with awk.
If you don't need exact output, perhaps you can use diff's --line-format=... directive to inject actual line number in a diff, rather than the word you removed in preprocessing step.

Is there any point in TortoiseSVN diff highlighting last lines of both files as changed?

In TortoiseSVN 1.5.1 I've noticed a curious pattern. If I change one line in a versioned file and then view difference the diff will highlight not only the changed line but also the last line of the file (though the last line hasn't changed and the colors in the highlight display that correctly).
Is there any use of this curious highlighting of the last line?
This bug was fixed in 1.5.7. See the changelog for details.
This could be a limitation of diff. Check if the file ends with a newline character.
GNU diff will mark incomplete lines (lines that are not terminated with a newline character) as changed, even if they have not because diff can't represent incomplete lines. See this diff doc
Some tools work around this limitation. Tortoise might not.
You could always check the last line to see if the file has changed. But I think you are right, pretty pointless :)
This is helpful, when the file has not been changed but the svn-metadata of the file have. When the diff highlights the last line without any previos change you can be sure that the file is unchanged and the metadata has changed.
Besides updating Tortoise, I would recommend using SourceGear DiffMerge. Tell Tortiose to use an external Diff Viewer and give it a command line like: C:\Program Files\SourceGear\DiffMerge\DiffMerge.exe /t1=%bname /t2=%yname %base %mine.
It'll look like Tortoise but work much better.

multiple changes in one line with diff tool?

Normally, 'diff' tool finds only changes between lines. For example, if i compare 'abcdef' and 'AbcdEf', diff will show that 'abcde' is changed and 'f' is unchanged. Is it possible to find multiple changes per line, so in example above i will see that it's only 'a' changed to 'A' and 'e' changed to 'E'? Or diff outut format does not support such?
There are multiple diff tools that will do what you're asking for.
Off the top of my head I know Winmerge and TortoiseMerge does that.
I recommend KDiff3 which highlights with different colours changes on the same line.
I wrote a tool to diff web code regardless of differences from comments and whitespace. This means my tool can diff a completely minified file against a similar beautified file. It is written entirely in JavaScript so you try it directly in your browser without downloading or installing anything. This does highlight differences per line and highlights differences per characters on those lines.
http://prettydiff.com/