Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to Using Perl Regex to find & replace large chunks in code by jedikaiti

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-23 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found