Your while loop makes more sense, so I'm going to build from there. Look at your list of substitutions:

s/^[ ]*//g; s/[ ]*$//g; s/\:000[A,P]M//g; s/99991231//g; s/Jan 1 1900 12:00:00//g; s/[ ]*\~\t\~[ ]*/~/g;
I see that all but the last one are removing text, so why not combine them into one pass? Also, youre first two appear to be designed to trim leading and trailing spaces from lines: there's a pretty standard way to do this (s/^\s+|\s+$//g), so I took the liberty of using that structure below. My trailing 'x' causes whitespace to be ignored, so I've replaced whitespace in matches with '\s'; you may want \0x20 instead, I don't know.

<update>Modified code with ikegami's suggestions from below. I chose '\ ' as the method to escape a space.</update>

while ( <IN_FILE> ) { s{ (?:^\ +|\ +$) |(?:\:000[A,P]M) |(?:99991231) |(?:Jan\ 1\ 1900\ 12:00:00) }{}gx; s/[ ]*\~\t\~[ ]*/~/g; print OUT_FILE $_; } close IN_FILE; ## and unlink() the filename for IN_FILE ## then rename() outfile to infile.
This should reduce your exec time a little bit, as it makes two regex passes instead of six. However, I suspect the slowest thing going is really disk IO (it usually is, with file operations). Doing the "write to another file then rename" has typically been faster than in-place editing, for me.

If you're reading your file over a network... well, don't -- make a local copy, process it, and pass it back to the network location. That will nearly always be much faster than streaming IO over a network.

Yoda would agree with Perl design: there is no try{}


In reply to Re: Search and Replace. by radiantmatrix
in thread Search and Replace. by Doniv

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.