Identification of a search and replace operation in a file

Imagine that you have a source file in which someone renames one character to another with a simple text replacement. The diff output in this file is usually a rather long and not very useful identifier for what happened. What you want to know - it's just a change s/a/b/g, and if so, what a, and b- you're not going to talk about this tool in advance, it will be necessary for his work himself. (You can assume here that "a" and "b" will not contain newlines for the argument.)

  • What algorithm would you use to search and replace? (Easy)
  • How could you work if there were several text substitutions? (pretty easy)
  • How could you write it to work with diff output, without comparing with the source files? (rather harder)
  • How far can you do this, for example, matching regular expressions rather than searching / replacing? (Hard)
  • In a file that contains this kind of change along with others, can you come up with a heuristic that will create a sed expression and a significantly shortened diff? (Hard)

This is a question for those of you who like to develop algorithms, but it is freely based on a real tool that I would like to use, so if you know about a good command-line tool that makes any of the above, bonus points .;)

+3
source share
2 answers

Two options that I'm thinking of now:

  • /s/b/a/g ( )
  • diff, , , , (.. , a b , )
+1

Darcs ( ) , . / . , Haskell, .

+1

Source: https://habr.com/ru/post/1788442/


All Articles