tr/// is the transliteration operator, good for converting individual characters into some other character (or counting, or deleting individual characters). It's not helpful in this case.

You actually want to look at the s/// substitution operator, for what you're trying to accomplish. Here is a description of what to do:

open $filename for reading. Open a temporary file for output. Scan through $filename line by line using the while( <INFILE> ) {..... construct. Inside the loop, on each line, use the substitution operator to identify matches, and substitute for your new text. Then print each line back out to your temp file. After the loop terminates, close both file handles, and rename the temp file to replace the original file.

If you're operating in a multi-user or web server context, file locking will also be advisable.


Dave


In reply to Re: search and replace text in multiple files in a directory by davido
in thread search and replace text in multiple files in a directory by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.