I'm trying to replace in one of my projects all mysql to mysqli, with their parameters. To do so, I'm searching for all files with mysql in my folder recursively and replacing it with mysqli.
mysql_query($query) to mysqli_query($link, $query)
I'm able to capture everything (according to Regex tester) but nothing seemed to be replaced, and no errors as well.
grep -rl mysql PROJECTFILE | xargs sed -ri "s/(mysql_query)\(\$([\w-]+)\)/WhatIWant/g"
I have read about backslashes in sed but didn't quite get it; the output has no errors but the file remains the same. If I can only know what is missing with an example or explanation, that would be perfect.
It's easy to replace mysql to mysqli, but it would be nice to do it all at once. I'm using a temporary file just to make sure it works before I can try it on in my project.
EDIT: I know if I want to do it all at once I have to capture mysql on its own and add an i, but that was just a thought.
For tedron, my desired output would be similar to this.
sed -ri "s/(mysql_query)\(\$([\w-]+)\)/mysqli_query($link, \2)/g" filename
glenn suggested a solution with no capturing. However, if I want to modify mysql_fetch_array($query), then I may have to use regex to keep the first parameter and append MYSQLI_BOTH for example.
Thanks to tedron, I was able to solve my problem according to my way:
sed -ri 's/(mysql)_query\((\$[a-zA-Z_-]+)\)/\1i_query($link, \2)/' filename
And for mysqli_fetch_array:
sed -ri 's/(mysql)(_fetch_array)\((\$[a-zA-Z_-]+)\)/\1i\2(\3, MYSQLI_BOTH)/' filename
Use this with
grep as I did in my question to modify a directory at once.