Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: Massive File Editing

by Kage (Scribe)
on Dec 15, 2002 at 07:17 UTC ( [id://219970]=note: print w/replies, xml ) Need Help??


in reply to Re: Massive File Editing
in thread Massive File Editing

Nothing unexpected, I just can't make any good way to replace every instance of an anchor href of "main.php?page=..." to "/?id=..." in every .shtml file.
“A script is what you give the actors. A program is what you give the audience.” ~ Larry Wall

Replies are listed 'Best First'.
Re: Re: Re: Massive File Editing
by MarkM (Curate) on Dec 15, 2002 at 07:29 UTC

    The easiest solution (although not the most efficient) would be to invoke Perl from within Perl. For example, use the loops you already have defined to gather a set of path names into an array. Then, invoke:

    system('perl', '-I.bak', '-pe', 's[main.php\?page=][/?id=]g', @pathnam +es) == 0 or die "Subcommand failed with return code $?.\n";

    Try to make the regexp as accurate and complete as possible to avoid incorrect alterations. If you want a longer term solution that is a bit more efficient, see the perlrun manpage to see an example of the code that approximates the behaviour of -pI.bak.

    NOTE: If the above system() invocation does not work, try changing 'perl' to be $^X ($EXECUTABLE_NAME with 'use English'). If the regular expression becomes very complicated, it may be easier to store the command in a perl script, and use the sub-script name instead of -e '...' in the system() invocation.

Re: Re: Re: Massive File Editing
by Jaap (Curate) on Dec 15, 2002 at 10:21 UTC
    First open the file and read the content into a scalar
    Then do a
    $fileContent =~ s/(<a href=")main\.php\?page\=/$1\/\?id\=/g;
    And then write the new contents back to the same file (or a new one with an added extension like .new or so

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://219970]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found