Deleting a section of file using perl

Discussion in 'Perl Misc' started by Ami, Sep 26, 2007.

  1. Ami

    Ami Guest

    Hi All,
    I am reading and file and same time modifying it (Inline edit). I
    store a particular position of file on some criteria using tell() and
    than after reading some other stuff, i need to decide, if i want to
    delete the contents of file from previously stored position till
    current position.
    It would be great help, if some one can give me idea/code snippet to
    do it.
    I need a code to delete file contents from one position to another
    position and again continue processing file in same way.

    Thanks in advance for your help.
    Regards,
     
    Ami, Sep 26, 2007
    #1
    1. Advertising

  2. Ami

    Mumia W. Guest

    On 09/26/2007 02:24 AM, Ami wrote:
    > Hi All,
    > I am reading and file and same time modifying it (Inline edit). I
    > store a particular position of file on some criteria using tell() and
    > than after reading some other stuff, i need to decide, if i want to
    > delete the contents of file from previously stored position till
    > current position.
    > It would be great help, if some one can give me idea/code snippet to
    > do it.
    > I need a code to delete file contents from one position to another
    > position and again continue processing file in same way.
    >
    > Thanks in advance for your help.
    > Regards,
    >


    Inline editing doesn't mix too well with seek() and tell(), so you'll
    probably have to redesign the program to not use inline editing.

    If you have a line-oriented file, Tie::File is a great way to make a
    file look like an array of lines; you can then use array operations on
    that file and delete groups of lines at will.
     
    Mumia W., Sep 26, 2007
    #2
    1. Advertising

  3. Ami

    Guest

    Ami <> wrote:
    > Hi All,
    > I am reading and file and same time modifying it (Inline edit).


    By that do you mean -i (or $^I) ?

    > I
    > store a particular position of file on some criteria using tell() and
    > than after reading some other stuff, i need to decide, if i want to
    > delete the contents of file from previously stored position till
    > current position.


    Can you just delay printing that chunk, storing it in memory instead,
    until you make that decision? Then either print it or discard it. If you
    can't do it this way, rather than with tell or whatever, then I think that
    trying to use -i is a mistake.

    > It would be great help, if some one can give me idea/code snippet to
    > do it.
    > I need a code to delete file contents from one position to another
    > position and again continue processing file in same way.


    See the implementation of Tie::File. It is not easy, and will probably
    be slower than molasses.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Sep 26, 2007
    #3
  4. Ami

    Ami Guest

    On Sep 26, 9:08 pm, wrote:
    > Ami <> wrote:
    > > Hi All,
    > > I am reading and file and same time modifying it (Inline edit).

    >
    > By that do you mean -i (or $^I) ?
    >
    > > I
    > > store a particular position of file on some criteria using tell() and
    > > than after reading some other stuff, i need to decide, if i want to
    > > delete the contents of file from previously stored position till
    > > current position.

    >
    > Can you just delay printing that chunk, storing it in memory instead,
    > until you make that decision? Then either print it or discard it. If you
    > can't do it this way, rather than with tell or whatever, then I think that
    > trying to use -i is a mistake.
    >
    > > It would be great help, if some one can give me idea/code snippet to
    > > do it.
    > > I need a code to delete file contents from one position to another
    > > position and again continue processing file in same way.

    >
    > See the implementation of Tie::File. It is not easy, and will probably
    > be slower than molasses.
    >
    > Xho
    >
    > --
    > --------------------http://NewsReader.Com/--------------------
    > The costs of publication of this article were defrayed in part by the
    > payment of page charges. This article must therefore be hereby marked
    > advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    > this fact.


    Hi All,
    Thanks for your response. I have opted to go for using another
    output file and keeping track of what part of file is needed to be
    truncated. By using this method, i can serve my purpose.
    Regards,
     
    Ami, Sep 27, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,414
    Scott Allen
    Jun 4, 2005
  2. Harry Barker
    Replies:
    2
    Views:
    521
    Alf P. Steinbach
    Apr 19, 2006
  3. Markus Hänchen

    Perl not deleting file content if file on server

    Markus Hänchen, Jul 24, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    144
    Peter J. Holzer
    Jul 24, 2006
  4. kampy
    Replies:
    9
    Views:
    337
    Steven D'Aprano
    Oct 19, 2012
  5. crea
    Replies:
    2
    Views:
    417
    Nobody
    Dec 28, 2012
Loading...

Share This Page