Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

One-liner: -i without .bak?

by cryptoquip (Novice)
on Jul 21, 2005 at 18:10 UTC ( [id://476941]=perlquestion: print w/replies, xml ) Need Help??

cryptoquip has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks. A co-worker (running cygwin) needed a script to run a search and replace operation on a bunch of files in a directory. He knew I was learning Perl, so he asked me to help, and we came up with this:
-pi -e 's/^<div.*>/<div>/' *.xml
From what I've read, I thought that -i would not create backup files if no extension was specified, but backup files is what he got. Mysterious, no? Why did this happen, and how could we have made it not happen?

Replies are listed 'Best First'.
Re: One-liner: -i without .bak?
by gellyfish (Monsignor) on Jul 21, 2005 at 18:37 UTC

    I think you will find that this is something to do with the fact that you can't have anonymous filehandles on windows - i.e. the file can't be unlinked when it is open. I can't remember where this is documented though.

    /J\

      Aaah, I stand corrected. Looking through perl.c looks that on Cygwin it will automagically use .bak. The OP didn't specify platform but that's a plausible explanation.

      --
      We're looking for people in ATL

Re: One-liner: -i without .bak?
by ikegami (Patriarch) on Jul 21, 2005 at 19:57 UTC

    By the way, you might want to replace
    perl -pi -e 's/^<div.*>/<div>/' *.xml
    with
    perl -pi -e 's/^<div.*?>/<div>/' *.xml
    As it was, it would match until the last ">" on the line.

Re: One-liner: -i without .bak?
by ysth (Canon) on Jul 22, 2005 at 03:17 UTC
    -i without suffix is only possible if your OS can rename open files; Windows can't. Cygwin perl defaults to .bak (so ported scripts using just -i at least run, if a little differently), while Win32 perl requires a backup extension and complains "Can't do inplace edit without backup." if one is not given.
Re: One-liner: -i without .bak?
by Fletch (Bishop) on Jul 21, 2005 at 18:13 UTC

    Are you sure the backup files didn't get created during a prior run when -i.bak was given? Because -i won't randomly come up with a backup extension on its own.

    --
    We're looking for people in ATL

      He says he never ran it with an extension in place.
        Have you administered a polygraph exam? I smell a rat... a big Commie rat...


        No rats were harmed in the making of this post. No offense is intended towards Communists, their families or anyone who might know or think they know a Communist. Offer void in Utah. Full offense is intended towards anyone from Utah.... just kidding.

        The backups weren't created by this one-liner. -i will not create a bkup without an extension and the error's not anywhere else in your one-liner, just tested it. So see what other code is being ran on these xml files (check there creation dates versus when you run this one-liner?).

        "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://476941]
Approved by davidrw
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-24 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found