Re: win32 and modifying a file
Forgive me for being dense here for a moment, but isn't it the case that in any text file, or any kind of file for that matter, editing in place is overwriting data in subsequent bytes ? So every single time I had to modify the middle of a file I had to do exactly what you describe. It has nothing to do with Unix vs Windows, more with linear files. Angelos On 2012-06-08 00:08, Greg Aiken wrote: 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 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: win32 and modifying a file
From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Greg Aiken Sent: 07 June 2012 23:08 To: Perl-Win32-Users@listserv.activestate.com Subject: 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. I would call editing a file in place, without a backup, more of a hack. Depending on how valuable your data is, making it possible to perform a rollback in the event of a problem seems sensible. So, however you do it, keeping a backup of the original may be a good idea. As for the how to do it part, Tie::File has already been suggested, which may be a good fit for what you want to do. If your file is small enough to fit in memory, then File::Slurp may be worth a look, particularly the edit functions. HTH -- Brian Raven Please consider the environment before printing this e-mail. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: win32 and modifying a file
-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
Re: win32 and modifying a file
-8--- 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. -8--- Windows doesn't allow in place editing, so on DOS you're left with an extra move command. What I like using for this problem is Tie::File So long as the input file isnt too large (it trades memory for speed) this module works a treat. Just in ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: win32 and modifying a file
Date: Thu, 7 Jun 2012 15:08:08 -0700 Subject: win32 and modifying a file From: gai...@visioninfosoft.com To: Perl-Win32-Users@listserv.activestate.com 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. I'm not sure what the problem is, perl can't open a file for sysread and syswrite at the same time? Maybe you want a MMF? http://code.activestate.com/lists/perl-win32-users/33260/ ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs