find and replace string with regexp in multiple files using ack


New Member
Mar 4, 2020
Reaction score
Hi everyone !
I successfully can find unquoted string in my php files, recursively, with this command line:
ack --heading --php "\[[a-zA-Z0-9_]*]"

But then, now, how to add quotes to this string ?

(FYI, My goal is to replace all constant in my php files, recursively to get rid of the Warning: Use of undefined constant.)

Any suggestions ?

Any great command line experts could suggest a solution ?
Thanks !
I've never used ack. I've used awk. (bad humor)
Usually I use grep to search for strings.

For search and replace, I use sed.
The thing about sed is.. you need to know what you are searching for in advance.

sed -i 's/oldstring/"new string with quotes"/g' myfile.php

Just replace the old string with whatever you're searching for.
It can be an entire line of text.
Then add quotes in the "new string" section. Double quotes are easy.
single quotes need to be "escaped".
sed -i 's/\[\([a-zA-Z0-9_]*\)\]/["\1"]/g' file
Last edited:
backup code directory first. then try:

find code_directory/ -type f -exec sed -i 's/\[\([^]"]*\)\]/["\1"]/g' {} \;

Ok, you ARE a wizard. Thanks a lot !!!! Its works perfectly. Knowing regexp is like having superpowers.
I get a find: paths must precede expression: exec when I try to run this.
make sure any paths or names used are wrapped in quotes if "*" (asterisks) are included.

Members online

Latest posts