Created
December 25, 2020 20:02
-
-
Save ellbur/46f08fbe0f4d62f501c19c552c90c9de to your computer and use it in GitHub Desktop.
Find and replace in multiple files with Raku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/rakudo | |
# vim: ft=perl6 | |
use File::Temp; | |
my $here = $*PROGRAM.parent; | |
sub process-py-file($f) { | |
say $f; | |
my ($temp, $temp-fh) = tempfile; | |
for $f.lines -> $line { | |
my $fixed_line = S/foo/bar/ with $line; | |
$temp-fh.print: $fixed_line; | |
$temp-fh.print: "\n"; | |
} | |
$temp-fh.close; | |
copy $temp, $f; | |
} | |
sub process-file($f) { | |
if ($f.basename ~~ /\.py$/) { | |
process-py-file $f; | |
} | |
} | |
sub process($d) { | |
if ($d.d) { | |
for dir $d -> $child { | |
process $child; | |
} | |
} | |
else { | |
process-file $d; | |
} | |
} | |
process $here; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment