I have been asked to come up with a little something that could be used whenever we need to do a mass find-replace on large chunks of code. We're talking about CSTOL code that's edited in Emacs, and we need to swap out something like the below that shows up multiple times in anywhere from 1 - 10 (maybe more) different files.
; This code does something fancy enable fancy_command wait (fancy = true) or for $max_time if $$error = $time out write "Oh crap, the fancy thing didn't work!" deal with not working else do fancy_command write "fancy command done!" endif
I figured this would be do-able with regular expressions in Perl, but I have no idea how, as I've never tried to match more than a single line at a time.
So I consulted my good friend Google and found this: http://noctilucent.org/blog/2003/12/replacing-large-chunks-of-text-with-perl.html
So I gave that solution a shot. It seems to spit out a chunk of reasonable-looking regex into sub.pl, but beyond that does nothing, and I'm really not sure how the sub.pl output is supposed to work. For my example "code" above, I get this in sub.pl:
;\s+This\s+code\s+does\s+something\s+fancy\s+ enable\s+fancy_command\s+ wait\s+\(fancy\s+=\s+true\)\s+or\s+for\s+\$max_time\s+ if\s+\$\$error\s+=\s+\$time\s+out\s+ write\s+"Oh\s+crap,\s+the\s+fancy\s+thing\s+didn't\s+work!"\s+ deal\s+with\s+not\s+working\s+ else\s+ do\s+fancy_command\s+ write\s+"fancy\s+command\s+done!"\s+ endif\s+%TESTING %six
And, according to the web page, I can run that on my files (assuming similar file names) with this command: perl -p -0777 -i.bak sub.pl filename*.prc When I try that, nothing happens. My file doesn't get changed in any way (although the last modified date/time does get updated), but it does make a nice backup copy.
So, I'm lost. Can anyone help me find my way?
In reply to Using Perl Regex to find & replace large chunks in code by jedikaiti
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |