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

Hi,
In a particular file how do I remove and insert and like.I have to remove a line comtaining

"...Row AutoFitHeight="0"><Cell><Data ss:Type="Strin.."
and then use file pointer to insert another record in the same place.
Thank u

20060128 Janitored by Corion: Added formatting

Replies are listed 'Best First'.
Re: remove and insert
by McDarren (Abbot) on Jan 28, 2006 at 02:39 UTC
    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 :)

Re: remove and insert
by BrowserUk (Patriarch) on Jan 28, 2006 at 02:52 UTC

    If your files aren't too huge, or even if they are and you are not in a screaming hurry, then Tie::File allows you to treat files as an array of lines and update them in-place.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: remove and insert
by perlsen (Chaplain) on Jan 28, 2006 at 06:33 UTC

    Hi Try this code for example,

    undef $/; open (INP, 'text.txt'); $string = <INP>; close INP; $string =~s#"...Row AutoFitHeight="0"><Cell><Data ss:Type="Strin.."#ne +wstring#gsi; open (OUT, ">".'text1.txt'); print OUT "$string"; close OUT;
    Regards
    perlsen