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

Perl command line search and replace

by Anonymous Monk
on Jan 05, 2008 at 03:09 UTC ( #660496=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

just how powerful is this option?

I wish to edit a bunch of .html files inside a directory and basically searching and replacing html coding.

To do this, I plan on using:

perl -pi -e 's/http://www.mydomain.com/script.pl?key=3/http://www.mydomain.com/script.pl?key=4/' /home/name/public_html/html/*.html

Now would the above work if I was to run the command via cron job on a shared hosting environment? I also may be replacing html with quotes inside the search pattern. Do I cancel those out like I would when writing in perl?

For ex:
perl -pi -e 's/<font color=\"blue\">/<font color=\"red\">' /home/name/ +public_html/html/*.html


I'm going to do this later on with trial and error, but want to make sure if I'm on the right track with what I'm trying to do.

Thanks.

Comment on Perl command line search and replace
Select or Download Code
Re: Perl command line search and replace
by Anonymous Monk on Jan 05, 2008 at 03:46 UTC
    wrong track write a program
Re: Perl command line search and replace
by dynamo (Chaplain) on Jan 05, 2008 at 03:57 UTC
    imo, if you want to run it as a cron job, it's worth making it into a script so that you don't have to lose one of your two main quoting mechanisms in just writing the program.

    Also, you will have to be aware of shell-based quoting and interpolation rules as a first layer of evaluation before your perl program gets to it. For example if you had a variable called $HOSTNAME, it would not start out undefined on unix systems. It wouldn't even get to perl still being a variable - unless you start backslashing(cancelling) the $. Basically all $s. And if you want to print one, you start to have to worry about whether you want to backslash the backslash that comes before the backslashed $ (you do, IIRC.)

    Unless you take pleasure in excessive unnecessary complication (a common affliction in these halls, I know,) I say write a short script if at all possible, and just call it from cron. This way you can use the full range of quoting mechanisms the shell offers you to express your search and replace terms.

    - d

      Actually, if the system shell is sh or bash, the extreme problem with every $ variable in the perl script needing to be backslashed as \$ only applies if the script is in double quotes. A single quoted script will be passed intact to perl, needing backslashes only in front of single quotes.

      For instance, the nasty

      perl -wle "\$x=42; print \$x"
      can be written as a fairly tolerable
      perl -wle '$x=42; print $x'
Re: Perl command line search and replace
by hipowls (Curate) on Jan 05, 2008 at 04:30 UTC

    You will also want to check those regular expressions. As they are they won't even compile. URLs contain lots of /s so you will probably want to choose a different delimiter, for example | or {}. Don't forget that . and ? are regex meta characters.

    Something along the lines of will be better

    s{\Qhttp://www.mydomain.com/script.pl?key=3\E} {http://www.mydomain.com/script.pl?key=4} s{\Q<font color="blue">\E} {<font color="red">}
    By using the \Q \E pair you remove the need for most of the \ escapes and let perl handle the special characters. Aligning the replacement text with the original text also makes it easier to see what you are changing (I'm assuming that you have been convinced to put this in a file).

      So if we are writing a simple script (I am assuming the script's name is test.pl):

      s{\Qhttp://www.mydomain.com/script.pl?key=3\E} {http://www.mydomain.com/script.pl?key=4}

      filename we are changing is called web.sh

      Would I write it from the command line like the following:

      perl test.pl web.sh

Re: Perl command line search and replace
by swampyankee (Parson) on Jan 05, 2008 at 14:53 UTC

    Given the vagaries of html, the consensus of Monks seems to be that it's generally a bad idea to try to edit html with regex; you're better off using one of the CPAN modules.

    I would also suggest that a formal Perl program, vs a one-liner, as it's likely that somewhere along the way somebody will say "you know that program you've got fixing this html...well, it also needs to do this..."


    emc

    Information about American English usage here and here.

    Any Northeastern US area jobs? I'm currently unemployed.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-07-11 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (224 votes), past polls