This is a specific example of a general subject I fail to grasp.
For years, I have used regex and sed to find/replace all occurrences of a string in all the files in a directory recursively, using something like this:
#FIND $GLOBALS['timechecks'] and REPLACE with completely_different_string shopt -s globstar dotglob; for file in /var/www/**/*; do if [[ -f $file ]] && [[ -w $file ]]; then sed -i -- 's/\$GLOBALS\['\''timechecks'\''\]/completely_different_string/g' "$file" fi done
The problem is, there is something basic about using Regex in bash I have got away without knowing. As a result, I cannot figure out a solution to a particular example.
TARGET STRING WHERE I AM STUCK
$GLOBALS['timechecks']=addTimeCheck_sparky($GLOBALS['timechecks'], number_format(microtime(true),6,'.',''), __LINE__, basename(__FILE__));
REGEX I CAME UP WITH NOT WORKING
This is just the sed line from my script with the search regex I came up with, to no avail.
\$GLOBALS\['\''timechecks'\''\]=addTimeCheck_sparky[(]$GLOBALS\['\''timechecks'\''\][,][ ]number_format[(]microtime[(]true[)][,]6[,]'\''\.'\''[,]'\'''\''[)][,][ ]__LINE__,[ ]basename[(]__FILE__[)][)][;]
I used a regex debugger for this example, which shows the regex finding my target string, but it is not working for me. The debugger is at this link. Here is the regex it shows finding my target string:
\$GLOBALS\['timechecks\'\]=addTimeCheck_sparky\(\$GLOBALS\[\'timechecks\'\], number_format\(microtime\(true\),6,\'\.\',''\), __LINE__, basename\(__FILE__\)\)
PROBLEMS WITH OUTPUT FOR REGEX DEBUGGER:
First, I tried my regex in the de
- I don't know why the debugger's regex works when I run it there, but not in my bash script.
- The regex looks "wrong" when compared to what I have learned to use for regex in bash with sed
- The regex from the debugger does not work when I plug it into the script I use for doing this task.
- Since I don't understand it, I cannot fix it
I think the basic problem I am clueless about converting valid regex from the debugger to work in bash/sed.
I searched for "how to use regex with sed in bash," but have not found an explanation of the fact this is even a potential problem.
Related question: Why is there no generator that accepts the target string as input and provides the regex that will find it?