I have special file format where I need to replace dozens of strings and reformat its structure. As the simplest solution I have prepared my patterns file where all regex definitions/replacements are stored (~100 replacements). I'm using perl to find and replace patterns (perl -p patterns source.file). Everything so far so good.
However, there is one case I'm unable to resolve using regex. I need to replace strings in part of the whole line, i.e. replace string in within a sub-string only.
Example: For simplicity, I need to replace all "A" to "X" only in the middle string (delimited by ;).
Input line:
ABCD ABCD; ABCD ABCD; ABCD ABCD
Expected output:
ABCD ABCD; XBCD XBCD; ABCD ABCD
^ ^
the only replaced characters
This correctly replaces all characters:
s/A/X/g;
But I need to replace commas in the middle field only. I tried:
s/(.*?;.*?)A/\1X/g;
s/(.*?;.*)A(.*?;)/\1X\2/g; # alternative to find the last A
But this replaces either the first A. I can have multiple patterns like this to repeat the search&replace procedure but this does not sound like a good solution as I don't know how many A's I will have in the sub-string.
I tried to play with lookbehind but unsuccessfully. Please note, I just need a regex definition I could use in my patterns file (i.e. no perl code). Alternatively, I'm able to use sed or awk to handle this case but I'm not too much familiar with it.
Thanks, community!
Regex101: https://regex101.com/r/Ic4ciA/1
sedandawk, or is there a programming language which you can use here?bashscript so I can just any simple command to the pipeline. I think I would be able to handle this using some code (I'm pretty familiar withpython). But I'm more curious if it's possible to match&replace the strings with some special regex pattern.