Modify a text file directly

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Modify a text file directly

    Hi all,

    I have to replace some chuncks out of a text file. My actual way to do this
    is to put the file contents into a scalar variable :

    open (INPUT, "$ARGV[0]") or die ("Can't open file : $!\n");
    $in = <INPUT>;
    close INPUT;

    Then I do my modifications to the file and I (must) write the replacements
    into another file :

    open (OUTPUT, ">$ARGV[1]") or die ("Can't create file : $!\n");
    seek (OUTPUT,0,0);
    print (OUTPUT $in);
    close OUTPUT;

    That way, I must save the replacements to ANOTHER file, which I don't want.
    I would like to modify the file DIRECTLY. Since I cannot modify the file
    using the file handle like this :

    <INPUT> = s/blahblah/blah2blah2/g

    How could I modify the file directly?

    Thanks in advance,

    Steve
    Steve Hémond Guest

  2. Similar Questions and Discussions

    1. Is there an easy way to just modify text content with VB
      Hello, I just want to change any given text in a PDF document programatically. For instance, changing the language of the text within a PDF document...
    2. Adding text directly into keyframes, creating tweens
      Hello, I would like to create a Word Document and import that word document into Flash. I'm not talking about data integration from an outside...
    3. How to *modify* text in HTML::Element
      Sorry for the repost, I hit Send too soon. I am using HTML::TreeBuilder to futz with some HTML, and I need to modify the text of some of the...
    4. text modify
      Hi, I have some data column based data I want to modify a bit. 0065 663 517 046 0 1485 0065 663 517 046 3 1500 0065 663 517 046 5 1882...
    5. Modify Text in a Combo Box A2002
      I'm trying to validate and change (if necessary) what a user types in a combo box, specifically a person's name. The bound combo box presents...
  3. #2

    Default Re: Modify a text file directly

    On Thu, 18 Sep 2003 16:58:23 -0400,
    Steve Hémond <shemond@hotmail.com> wrote:
    > Hi all,
    >
    > I have to replace some chuncks out of a text file. My actual way to do this
    > is to put the file contents into a scalar variable :
    See the Perl FAQ, Section 5, question

    "How do I change one line in a file/delete a line in a file/insert a
    line in the middle of a file/append to the beginning of a file?"

    Martien
    --
    |
    Martien Verbruggen | This matter is best disposed of from a great
    Trading Post Australia | height, over water.
    |
    Martien Verbruggen Guest

  4. #3

    Default Re: Modify a text file directly

    Steve Hémond wrote:

    ....

    > I have to replace some chuncks out of a text file. My actual way to do this
    > is to put the file contents into a scalar variable :
    >
    > open (INPUT, "$ARGV[0]") or die ("Can't open file : $!\n");
    > $in = <INPUT>;
    > close INPUT;
    >
    > Then I do my modifications to the file and I (must) write the replacements
    > into another file :
    >
    > open (OUTPUT, ">$ARGV[1]") or die ("Can't create file : $!\n");
    > seek (OUTPUT,0,0);
    > print (OUTPUT $in);
    > close OUTPUT;
    >
    > That way, I must save the replacements to ANOTHER file, which I don't want.
    > I would like to modify the file DIRECTLY. Since I cannot modify the file
    > using the file handle like this :
    >
    > <INPUT> = s/blahblah/blah2blah2/g
    >
    > How could I modify the file directly?

    Couple of things you should check out:

    1. The -i switch on the perl command. See perldoc perlrun for details.
    Also check out the -p and -n switches while you're at it. Something
    on the order of

    perl -i.bak -pe "s/blahblah/blah2blah2/g" filename.ext

    might suffice. Of course, behind the scenes, something similar to what
    you wrote is happening (although the file isn't being slurped) -- this
    is just a very convenient shorthand for folks with the virtue of laziness.

    2. The Tie::File module will let you tie a file to an array, with one
    line per array element. You may modify the array elements, which will
    cause the file to be modified. Again, behind the scenes, this is still
    actually doing something somewhat similar to what you were originally
    doing -- there is no other way to accomplish it, unless you happen to be
    replacing stuff character-for-character (in which case check out seek
    and tell).


    ....

    > Steve
    --
    Bob Walton

    Bob Walton Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139