Why do I have to specify the -i switch with a backup extension when using ActivePerl?
This is a Windows/MS-DOS limitation. According to perldiag:
You're on a system such as MS-DOS that gets confused if you try reading from a deleted (but still opened) file. You have to say -i.bak, or some such.
Perl's -i
implementation causes it to delete file1.txt
while keeping an open handle to it, then re-create the file with the same name. This allows you to 'read' file1.txt even though it has been deleted and is being re-created. Unfortunately, Windows/MS-DOS does not allow you to delete a file that has an open handle attached to it, so this mechanism does not work.
Your best shot is to use -i.bak
and then delete the backup file. This at least gives you some protection - for example, you could opt not to delete the backup if perl
exits with a non-zero exit code. Something like:
perl -i.bak -ape "splice...." file1.txt && del file1.bak
Related videos on Youtube
Zaid
profile for Zaid on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/45444.png I wear many hats at work: Mechanical Engineer Developer Tester Team Leader Algorithms Analytics Optimization Data Visualization And many hats in life: Dad DIY Mechanic Handyman
Updated on November 12, 2020Comments
-
Zaid over 3 years
I cannot get in-place editing Perl one-liners running under ActivePerl to work unless I specify them with a backup extension:
C:\> perl -i -ape "splice (@F, 2, 0, q(inserted text)); $_ = qq(@F\n);" file1.txt Can't do inplace edit without backup.
The same command with
-i.bak
or-i.orig
works a treat but creates an unwanted backup file in the process.Is there a way around this?
-
Zaid about 14 yearsSo it is a Windows limitation. I was hoping that I wouldn't have to
unlink
the backup with a separate command... looks like I'll have to. Thanks for the help. -
Mike about 10 yearsNot entirely related, but in-place editing on sed in windows seems to work for me. I can see that your example wouldn't work in sed, but it might be helpful to know.
-
zb226 over 7 years@Mike: On my end,
sed
in-place editing leaves temporary files like e.g.sedGihEwg
in the file system - yours doesn't? Which version have you got? I'm usingGNU sed version 4.2.1
. -
antred about 6 years@Zaid IMO it's a poo Perl implementation rather than a Windows limitation. If you port a program to another OS, your port has to account for the quirks of that OS, and Perl doesn't seem to bother in this case.
-
antred about 6 yearsAnd by poo I mean poor. Sorry.