Skip to content

Instantly share code, notes, and snippets.

@ellbur
Created December 25, 2020 20:02
Show Gist options
  • Save ellbur/46f08fbe0f4d62f501c19c552c90c9de to your computer and use it in GitHub Desktop.
Save ellbur/46f08fbe0f4d62f501c19c552c90c9de to your computer and use it in GitHub Desktop.
Find and replace in multiple files with Raku
#!/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