I’ve got a large number of PHP files and lines that need to be altered from a standard
echo "string goes here"; syntax to:
custom_echo("string goes here");
This is the line I’m trying to punch into Perl to accomplish this:
perl -pi -e 's/echo \Q(.?*)\E;/custom_echo($1);/g' test.php
Unfortunately, I’m making some minor syntax error, and it’s not altering “test.php” in the least. Can anyone tell me how to fix it?
Why not just do something like:
I don’t think
\Qand\Eare doing what you think they’re doing. They’re not beginning and end of quotes. They’re in case you put in a special regex character (like.) — if you surround it by\Q ... \Ethen the special regex character doesn’t get interpreted.In other words, your regular expression is trying to match the literal string
(.?*), which you probably don’t have, and thus substitutions don’t get made.You also had your
?and*backwards — I assume you want to match non-greedily, in which case you need to put the?as a non-greedy modifier to the.*characters.Edit: I also strongly suggest doing:
This will create a “backup” file that the original file gets copied to. In my above example, it’ll create a file named
file.php.bakthat contains the original, pre-substitution contents. This is incredibly useful during testing until you’re certain that you’ve built your regex properly. Hell, disk is cheap, I’d suggest always using the-pi.bakcommand-line operator.