Well, I guess the first thing you would want to do is open your original file (lets call this your INFILE).

Then you might want to create a new file to write your result into - OUTFILE, perhaps?

Then you'd probably want to iterate through your INFILE line by line until you found the line that matches. If the line didn't match, you'd simply print it to your OUTFILE, but if it did match, then you'd instead print the replacement text to your OUTFILE.

There are a few ways you could do the iteration. You might read the whole file into an array, and then use a foreach loop, or you might read it one line at a time with a while loop.

As far as finding the right line goes, you'll almost certainly need to use a regular expression

And of course, if you have a look at the doco for perlrun, you'll find that Perl has a nifty in-place file editing feature (-i), which allows you to do all of the above quite easily from the command line.

So... there's a basic outline of what is needed and some links to help you. Why don't you now have a go a writing it, and then come back and ask if you get stuck with any particular part (don't forget to show us the code that does/doesn't work).

Hope this helps,
Darren :)


In reply to Re: remove and insert by McDarren
in thread remove and insert 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.