Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Using Perl Regex to find & replace large chunks in code

by jedikaiti (Friar)
on Mar 11, 2013 at 18:03 UTC ( #1022840=perlquestion: print w/ replies, xml ) Need Help??
jedikaiti has asked for the wisdom of the Perl Monks concerning the following question:

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?

Kaiti
Swiss Army Nerd

Comment on Using Perl Regex to find & replace large chunks in code
Select or Download Code
Re: Using Perl Regex to find & replace large chunks in code
by ikegami (Pope) on Mar 11, 2013 at 18:54 UTC
    Actually, you should have gotten
    s% ;\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

    which can be used as follows:

    perl -i~ -0777p sub.pl *.cstol

    -0777 causes the whole file to loaded into $_.

    Update: Oops, that command is the same as you gave. I've just tested it and it works. Perhaps you didn't have a newline after the last endif?

      I will make sure I have that newline, thanks!
      Kaiti
      Swiss Army Nerd
Re: Using Perl Regex to find & replace large chunks in code
by ikegami (Pope) on Mar 11, 2013 at 19:01 UTC
    As a standalone script
    local $/; my $search = <>; my $replace = <>; #local $^I = ''; # No backups. local $^I = '~'; # Make backups. while (<>) { s/\Q$search/$replace/g; print; }

    Usage:

    $ script.pl - - *.cstol search search search ^D replace replace replace ^D
    or
    $ script.pl search.txt replace.txt *.cstol

    You can tweak $search's content as the linked node did if you so desire (but get rid of \Q if you do).

Re: Using Perl Regex to find & replace large chunks in code
by Anonymous Monk on Mar 11, 2013 at 19:57 UTC
      Thanks, both of you! I will play with that, too!
      Kaiti
      Swiss Army Nerd

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022840]
Approved by ChuckularOne
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-07-24 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (157 votes), past polls