I don't know any Perl, but I do occasionally use "perl pie" (perl -pi -e
) to do a batch regex find and replace, e.g. change a
to e
in all the .txt files in a folder perl -pi -e 's|a|e|g' *.txt
.
Is there any way to do a "dry run" so that I can preview the change? I'm frequently using moderately complex regexes with positive / negative lookaheads / lookbehinds, group references, etc., and I'm often not 100% sure I have it right on the first run. It would be wonderful to have something like rename.pl's -n
flag, which doesn't change anything, only outputs the changes that would have been made.
Currently, my strategy is to just use ack, which accepts a Perl regex, to make sure my match string is correct, and go from there. Is there a better way? Anything like rename -n
?
Also open to something other than perl
if I'm missing something awesome, though I am partial to its regex format.
Many thanks!
Just remove the -i
parameter from your perl code to do a dry run.
perl -pe 's|a|e|g' *.txt
From perl --help
,
-i[extension] edit <> files in place (makes backup if extension supplied)
To print the particular line where the replacement occurs.
$ cat file
abcd
cbcb
foo
$ perl -nle 'print if s|a|e|g' file
ebcd
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With