FAQ
Hi,

I would like to replace a part of a really big (4GB) text file. And
the contents that I want to change is really a small but continuous
portion. Could some one please help me with the best way I can do this
in perl?

Thanks,

Search Discussions

  • Jeff Pang at Oct 22, 2007 at 11:27 am

    On 10/22/07, anand.shankar@gmail.com wrote:
    Hi,

    I would like to replace a part of a really big (4GB) text file. And
    the contents that I want to change is really a small but continuous
    portion. Could some one please help me with the best way I can do this
    in perl?
    use perl one-liner,

    perl -pi.bak -e 's/foo/bar/' file

    or just read the file line by line and print the (modified) content to
    a new file,

    open RD,"file1" or die $!;
    open RW,">file2" or die $!;

    while(<RD>) {
    s/foo/bar/;
    print RW;
    }

    close RW;
    close RD;
  • Rob Dixon at Oct 22, 2007 at 11:34 am

    anand.shankar@gmail.com wrote:

    I would like to replace a part of a really big (4GB) text file. And
    the contents that I want to change is really a small but continuous
    portion. Could some one please help me with the best way I can do this
    in perl?
    You need to tell us more about the problem, but if you need to do this
    just once then do this:

    - Write a program to copy the file line by line to a new file

    - Change that program so that it recognizes and alter the line
    that must be changed before it is output


    If you need to do it on a regular basis then your data shouldn't be
    in a gigantic text file and you should look for other options.

    Rob
  • Dr.Ruud at Oct 22, 2007 at 5:36 pm

    anand.shankar@gmail.com schreef:

    I would like to replace a part of a really big (4GB) text file. And
    the contents that I want to change is really a small but continuous
    portion. Could some one please help me with the best way I can do this
    in perl?
    Is the replacement guaranteed to be the same size as the original?

    --
    Affijn, Ruud

    "Gewoon is een tijger."
  • The_train_man at Oct 23, 2007 at 9:23 pm

    On Oct 22, 10:32 am, rvtol+n...@isolution.nl (Dr.Ruud) wrote:
    anand.shan...@gmail.com schreef:
    I would like to replace a part of a really big (4GB) text file. And
    the contents that I want to change is really a small but continuous
    portion. Could some one please help me with the best way I can do this
    in perl?
    Is the replacement guaranteed to be the same size as the original?

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Thanks for your suggestion.

    No, the resulting file may be different in file length.

    To be more specific,

    Consider the file contents as:

    A
    B
    C
    D
    E
    F
    G
    H

    and I want to replace the lines 2-4 which may result in a file being

    A
    X
    Y
    Z
    D
    E
    F
    G
    H

    the problem that I am facing is the time it takes to write out the
    line 5 to end of file in cases where the file size run into GBs.

    Is there a way I can cat the contents from D to H to the modified top
    portion?

    Thanks,
  • Matthew Whipple at Oct 24, 2007 at 3:39 pm

    the_train_man wrote:
    On Oct 22, 10:32 am, rvtol+n...@isolution.nl (Dr.Ruud) wrote:

    anand.shan...@gmail.com schreef:

    I would like to replace a part of a really big (4GB) text file. And
    the contents that I want to change is really a small but continuous
    portion. Could some one please help me with the best way I can do this
    in perl?
    Is the replacement guaranteed to be the same size as the original?

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Thanks for your suggestion.

    No, the resulting file may be different in file length.

    To be more specific,

    Consider the file contents as:

    A
    B
    C
    D
    E
    F
    G
    H

    and I want to replace the lines 2-4 which may result in a file being

    A
    X
    Y
    Z
    D
    E
    F
    G
    H

    the problem that I am facing is the time it takes to write out the
    line 5 to end of file in cases where the file size run into GBs.

    Is there a way I can cat the contents from D to H to the modified top
    portion?

    Thanks,

    If you're on a *NIX then initially use "split". Modify the relevant
    file, and then you can just use cat old_end >> new_start. Otherwise
    there are similar utilities for other platforms (I think hjsplit should
    work for windows).
  • Ron Bergin at Oct 25, 2007 at 7:41 am

    On Oct 22, 1:52 am, anand.shan...@gmail.com (Anand Shankar) wrote:
    Hi,

    I would like to replace a part of a really big (4GB) text file. And
    the contents that I want to change is really a small but continuous
    portion. Could some one please help me with the best way I can do this
    in perl?

    Thanks,
    use Tie::File;

    http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedOct 22, '07 at 8:53a
activeOct 25, '07 at 7:41a
posts7
users6
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase