I want to automate moving duplicate or similar C code into functions.
This must work under Linux.
Be aware that you can't just compare lines of text. You will have to parse the code, in this manner, you could also detect segments that are semantically correct but may have different named identifiers.
For example, given two functions that are equivalent but use different identifiers, a text search will not see them as identical, but a parser can.
Also note that writing a C++ parser is not a trivial task, even when given the grammar. I suggest the advice of others and seek out a tool for this. Also search for refactoring tools.