> -----Original Message-----
> From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-
> win32-users-boun...@listserv.activestate.com] On Behalf Of Greg Aiken
> Sent: Thursday, June 07, 2012 4:08 PM
> To: Perl-Win32-Users@listserv.activestate.com
> Subject: [EXTERNAL] win32 and modifying a file
> 
> dear win32 perl users, ive never actually known how to 'modfiy' a file using
> perl on win32.  up till now, ive always read file1, found the data i intended 
> to
> change, and have always created a new file 2 containing the changes.  if i
> wanted to 'simulate' changing file1, when done i could rename file2 to file1.
> in other words, ive never learned how to modify a file directly.
> 
> ive read win32 makes it more difficult to do this than on unix os's.
> 
> but in any case, today i wanted to ask the group.
> 
> assumming 'file1' exists with the following 3 records in it:
> 
> A
> B
> C
> 
> is there a 'simple' code fragment someone could post that would
> demonstrate iterating through this file and when record 'B' is encountered,
> we want to change 'B' to 'B_modified' - done in a way where we only access
> 'file1'.  maybe this cant be done, but im asking.
> 
> yes i do realize there is another approach, upserp contents of 'file1' modify 
> in
> memory, delete 'file1', then recreate it by dumping the in-memory modified
> contents.  this seems more like a 'hack' than a direct manipulation of the
> original file.
> 
> anyways, thanks to any who might be willing to enlighten me about this
> topic.
> 
> greg

Possibly you want the Perl command line options:

       -i[extension]
            specifies that files processed by the "<>" construct are to be
            edited in-place.  It does this by renaming the input file, opening
            the output file by the original name, and selecting that output
            file as the default for print() statements.  The extension, if
            supplied, is used to modify the name of the old file to make a
            backup copy, following these rules:

            If no extension is supplied, no backup is made and the current
            file is overwritten.

            If the extension doesn't contain a "*", then it is appended to the
            end of the current filename as a suffix.  If the extension does
            contain one or more "*" characters, then each "*" is replaced with
            the current filename.  In Perl terms, you could think of this as:

                ($backup = $extension) =~ s/\*/$file_name/g;

            This allows you to add a prefix to the backup file, instead of (or
            in addition to) a suffix:

                $ perl -pi'orig_*' -e 's/bar/baz/' fileA    # backup to 
'orig_fileA'

            Or even to place backup copies of the original files into another
            directory (provided the directory already exists):

                $ perl -pi'old/*.orig' -e 's/bar/baz/' fileA # backup to 
'old/fileA.orig'

But, when I try the following one-liner attempting to overwrite my file under 
Windows7 CMD shell, I get an error:

        perl -pi -e "s/^B$/B_modified/" file1
        Can't do inplace edit without backup.

Note: This was run with ActivePerl v5.12.4 build 1205.

---------------------------

Also, when I tried running a similar command under Cygwin's Perl (v5.10.1), it 
behaves differently. It apparently defaults to assuming a ".bak" backup:

        perl -pi -e 's/^B$/B_modified/' file1

produces:
        file1           <-- the new file
        file1.bak       <-- the original file

If I try using the "*" construct referred to in the perldoc:

        perl -pi'*' -e 's/^B$/B_modified/' file1

it pretty much fails quietly as file1 ends up truncated as a zero length file 
(and there is no backup file).

---------------------------

So, maybe you can just live with this:

        perl -pi.bak -e "s/^B$/B_modified/" file1

It will first rename file1 to file1.bak, then process the file outputting each 
line into the output file, "file1". You will end up with two files though.

Note: I did not code any guard to only do this action if the file name was 
"file1". So you could pass any number of files to it, and it would iterate on 
each of them in similar fashion.

PS. Not sure that this is of any help to you as I ended up having two files. 
But at least it may illustrate some more power in Perl.

--
Mike Arms
ma...@sandia.gov


_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to