Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Put your inplace-edit backup files into a subdir

by merlyn (Sage)
on Apr 20, 2003 at 16:01 UTC ( #251836=snippet: print w/replies, xml ) Need Help??
Description: I just discovered that the inplace-edit value can now include an asterisk to mean "old file". This means you can now do cool tricks like put your "old" versions of the file into a subdirectory with the original name, not a modified name! Here's a one-liner that puts all the old files into a directory named for the unix epoch timestamp (a 10-digit integer increasing by one per second).
perl -pe 'BEGIN { mkdir $^T; $^I = "$^T/*" } s/foo/bar/g' file1 file2 
+file3 ...
Replies are listed 'Best First'.
•Re: Put your inplace-edit backup files into a subdir
by merlyn (Sage) on Apr 21, 2003 at 06:08 UTC
    I've had more than one person write to me in the day since I posted this snippet to say "Randal, you forgot the -i! It's not an in-place edit without that!".

    No, I didn't. Setting $^I is the programmatic equivalent to setting -i on the command-line.

    Cool, eh?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I was personally puzzled at what $^T is for a moment, though I knew about $^I. So the simple form would be
      mkdir bak/ ; perl -pi'bak/*' -e 's/foo/bar/g' file1 file2 file3 ...
      perl -pi'bak/*' -e 'BEGIN { mkdir($^I =~ m!(.*/)!) } s/foo/bar/g' file +1 file2 file3 ...
      Or maybe even
      perl -MFile::Path -pi'bak/*' -e 'BEGIN { mkpath [ $^I =~ m!(.*/)! ] } +s/foo/bar/g' file1 file2 file3 ...

      Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://251836]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-09-27 12:07 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (119 votes). Check out past polls.