I'm working on a sed replacement pattern that will take a file name with an absolute path and then print the unaltered input as well as the modified input (which should only be applied to the file name, not the path). Some path is guaranteed to be explicitly defined, so it will never be given something like "test.c", it would be "./test.c" in that instance.
I have some regex that is working, but only when the part to be substituted is at the start of the file name. I'd like it to work with any part of the file name.
To clarify, here's a summary of the current behavior.
INPUT PATTERN CURRENT OUTPUT DESIRED BEHAVIOR?
./test.c p;s;\(.*/\)t;\1###; ./test.c Yes
./###est.c
testdir/test.c p;s;\(.*/\)tes;\1#; testdir/test.c Yes
testdir/#t.c
./test.c p;s;\(.*/\)est;\1#; ./test.c NO
./test.c Should be ./t#.c
As you can see, this is my pattern:
p;s;\(.*/\)SubstringToFind;\1SubstringToInsert;
Here's my logic for this:
p
to print the unmodified line
s
for substitution (yeah, we all know that)
Capture the path: Any number of characters plus a final "/"
SubstringToFind: Look for a pattern somewhere in the file name.
\1
: Recover the unmodified path
SubstringToInsert: Insert the modified file name
I'm sure this just calls for one tiny little addition like one more ".*", which I've tried adding right after the capturing parentheses, but that just replaces the specified pattern along with all file name characters before it. I'm sure the solution is staring me in the face but I'm not seeing it. Any help from someone more seasoned in regex than myself? Thanks!