bobafifi has asked for the wisdom of the Perl Monks concerning the following question:

Anybody have a "Find and Replace" text script?
Need to go through 560+ pages and change text occurances of php? to html.

Many thanks in advance,

-Bob

Replies are listed 'Best First'.
Re: Find and Replace text script?
by gellyfish (Monsignor) on Jul 13, 2006 at 18:49 UTC

    You mean like:

    find . -name '*.html' | xargs perl -pi -e 's/php\?/html/g'
    ?

    /J\

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Find and Replace text script?
by Joost (Canon) on Jul 13, 2006 at 18:51 UTC
      > perl -i -pe 's/php\?/html/g' *

      Got it to work! :-)

      Thank you!!

      -Bob

Re: Find and Replace text script?
by saberworks (Curate) on Jul 13, 2006 at 20:40 UTC
    By php? do you mean you want to match php, php3, php4, phps, etc.? Or the literal string, 'php' followed by a literal question mark?
      > Or the literal string, 'php' followed by a literal question mark?

      yes the literal string - php?
      I've already changed the file names to .html using Automator - it's the internal links that have php? in them that I'm trying to change.

      Thanks,

      -Bob

        it's the internal links that have php? in them that I'm trying to change.
        That won't be so easy. It means that you first have to make sure that the php? you find is indeed an internal link and not part of something else (such as simple text: "Are you sure this is written in php?)").

        If you want the Monks to help you on this we will need more info. Can you show us some relevant parts of some of your files so one knows what we are dealing with?

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Find and Replace text script?
by duckyd (Hermit) on Jul 13, 2006 at 19:11 UTC
    avoiding xargs:
    find . -type f -name '*.html' -exec perl -pi -e 's/php\?/html/g' {} \;
      avoiding xargs

      Why spawn a copy of perl for each file?

      $ find . -maxdepth 1 | xargs perl -le 'print "$$: my args: @ARGV"' 12390: my args: . ./bash ./rbash ./sh ./cat ./chgrp ./chmod ./chown ./ +cp [...]
      $ find . -maxdepth 1 -exec perl -le 'print "$$: my args: @ARGV"' {} \; 12399: my args: . 12400: my args: ./bash 12401: my args: ./rbash 12402: my args: ./sh 12403: my args: ./cat 12404: my args: ./chgrp 12405: my args: ./chmod 12406: my args: ./chown 12407: my args: ./cp [...]

      --
      David Serrano

        Generally, I don't care if I'm spawning a new copy of perl or not. It runs plenty fast either way
      find . -type f -name '*.html' -exec perl -pi -e 's/php\?/html/g' {} \;

      hmmm ... when I run this, my computer *sounds* like it's processing the files and after about thirty seconds it stops and prints a new Terminal line. However, when I go to check the files, they remain unchanged. Do I need to change this "find . -type f -name" part somehow?

      Thanks,

      -Bob

        I am still unclear as to whether you're changing filenames from .php to .html, or changing all occurrences of the word "php?" to "html" within each file, or if you're trying to translate a PHP file to an HTML file (which doesn't really make sense, but the question really is ambiguous).

        Honestly, what are you TRYING to do? Please explain clearly.


        Dave

      Why avoid xargs? This is exactly the sort of thing this is for (or you may as well avoid find also and do the whole thing in perl).
        sorry, just posted it in the spirit of TIMTOWTDI