Reputation: 2719
This is a very basic question but I don't find the answer by googling it. I'm doing in a php program a simple call to perl to find and replace strings in many files:
exec("perl -pi -e 's|foo|bar|g' `find . -name *.foo`");
and I would like the command to return the number of replacements. Anyone knows how to do that? Thank you.
Upvotes: 0
Views: 846
Reputation: 399
try this :
exec("perl -pi -e 'END { print($c) } $c += s|foo|bar|g' find . -name *.foo
");
It'll print the number of replacements. You can then retrieve it from your exec. If you'd rather want the number to be in the return value of the process, you can try :
exec("perl -pi -e 'END { exit($c) } $c += s|foo|bar|g' find . -name *.foo
");
But as said before, it's strange to call Perl from PHP.
Upvotes: 1
Reputation: 118665
The s///
operator returns the number of substitutions that were made, so it is a simple matter of adding them together:
perl -pi -e '$C+=s|foo|bar|g; END{print"$C\n"}' `find . -name *.foo`
Upvotes: 2