I have a requirement to update a variable across multiple files/sub-directories. The variable to be replaced starts with the same 6 characters, everything after this is random. I will use these first 6 characters as the pattern to find/replace on. I will replace the random characters following with a sequential variable.
I'm not sure what utility is best to achieve this but I imagine sed in some kind of loop? I'm struggling to visualise how best to achieve this. I imagine it could be done with something like;
#!/bin/bash
i=0
grep -r '/parent/sub/' -e 'pattern' | while read line
do
sed 's/pattern*/pattern$i/g'
((i++))
done
My first issue is I don't know if sed can be used this way, secondly as it's nested in the loop how do can I feed it the required lines from the grep command (or is there a better method than grep to be used here?)
Thanks
sed
orawk
- however, note that the search pattern you indicated is that of "wildcards" (globs in the shell language), whereas forsed
you will need a "regular expression". Also, be careful that in your example, you would be replacing the pattern with literal$i
as variable expansion is disabled inside single quotes. – AdminBee Jul 09 '20 at 10:01pattern
in that context as it's ambiguous, always usestring
orregexp
, whichever one you mean. – Ed Morton Jul 10 '20 at 01:14