Re: File manipulation

From: FlashMX (FlashMX_at_sympatico.ca)
Date: 11/23/04


To: "beginners@perl.org" <beginners@perl.org>
Date: Tue, 23 Nov 2004 09:49:44 -0500

On Tue, 23 Nov 2004 09:44:00 -0500 (EST), Chris Devers wrote:

>On Tue, 23 Nov 2004, FlashMX wrote:
>
>> I'm trying to do this script on my own as suggested by someone in this
>> group.
>
>Glad to hear it :-)
>
>> I'm getting confused on the login.
>
>I don't see where the program or the problem description involves
>logging in to anything -- is that really what you meant, or do you mean
>some other concept

Doh...I meant to type "logic"

>
>> I need to open the orginal file (which I've done) and then do a grep
>> to search and then replace some text but I need to output the results
>> to the same file (can this be done?).
>
>Well, one easy way to do that is to use a temp file:
>
> * open original file
> * find what you want in the file
> * output the results to the temp file
> * move the temp file over the original file
>
>There are other ways to go about this, but this framework is one of the
>easier approaches to the problem.
>
>You could also iterate over the file, deleting each line that doesn't
>match the pattern you want, but this would be a little more complicated
>to write (not bad, mind you, just a little less straightforward) and I'm
>not sure that it would be any better than the steps outlined above.

I would still need to keep the original file intact. Bascially just reading in a large file. Using GREP to search for a match like "0000" then replace that with "0000 1111” and then search for
“2222” and then replace that with “2222 3333” and then start the search over again at “0000”...and so on at the same time writing the results to (I guess) a temp file and then rename the temp
file to the original when completed.

Being new I just want to make sure all my steps are correct.

 
>
>
>--
>Chris Devers



Relevant Pages

  • Re: fopen: pre-pending text
    ... >> 2) open a temp file for writing ... >> 4) write original file to end of temp file ... >right after verifiying. ... >> 8) open original file for writing at start of file ...
    (comp.os.linux.development.system)
  • Re: fopen: pre-pending text
    ... open a temp file for writing ... write original file to end of temp file ... verify temp file has desired data ... open original file for writing at start of file ...
    (comp.os.linux.development.system)
  • Re: Bash help requested: Capturing command errors within pipes
    ... The reason for this was because we sometimes needed to access variables by name at link-time and the CLI (Command Line Interface, a sort of macro processor) was case insensitive, so all commands caused everything to be flipped to uppercase, no matter what we wanted. ... and uniq'd original file to a temp file, ... => *)echo exit ok and PIPESTATUS all zeroes ...
    (Fedora)
  • Re: Modifying a file w/o creating a temp file
    ... ease of use, if the original file had hard links to it, they are ... space equal to the size of the temp file (unless the temp file is ... written to a different file system which raises its own issues). ... lost the file entirely. ...
    (comp.lang.java.programmer)
  • Re: Modifying a file w/o creating a temp file
    ... Of course there's one simple and obvious solution which has not yet been mentioned or rejected: read the whole file into memory, modify it there, truncate the original file, and write the modified data into it. ... This has substantial subtle benefits, balanced against the one flaw of being memory hungry and limited by swap space. ... Benefits include ease of use, if the original file had hard links to it, they are not broken, if it had special modes/attributes/ACLs, they are not lost, and it never needs more space than the file system has, whereas a temp-file solution typically needs an amount of unused disk space equal to the size of the temp file. ...
    (comp.lang.java.programmer)