Consider the following two files that are slightly different:
foo (old version):
<Line 1> a
<Line 2> b
<Line 3> c
<Line 4> d
foo (new version):
<Line 1> a
<Line 2> e
<Line 3> b
<Line 4> c
<Line 5> f
<Line 6> d
As you can see, characters e and f are introduced in the new file.
I have a set of line numbers corresponding to the older file…say, 1, 3, and 4 (corresponding to letters a, c, and d).
Is there a way to do a mapping across these two files, so that I can get the line numbers of the corresponding characters in the newer file?
E.G., the result would be:
Old file line numbers (1,3,4) ===> New File line numbers (1,4,6)
Unfortunately I have only emacs (with a working ediff), Python, and winmerge at my disposal.
What you need is a string searching algorithm where you have multiple patterns (the lines from the old version of foo) that you want to search for within a text (the new version of foo). The Rabin-Karp algorithm is one such algorithm for this sort of task. I’ve adapted it to your problem:
Assuming your two files are called
old_foo.txtandnew_foo.txtthen you would call this function like this:When I tried in on your data it printed: