I have changed up my director structure and I want to do the following:
- Do a recursive grep to find all instances of a match
- Change to the updated location string
One example (out of hundreds) would be:
from common.utils import debug --> from etc.common.utils import debug
To get all the instances of what I’m looking for I’m doing:
$ grep -r 'common.' ./
However, I also need to make sure common is preceded by a space. How would I do this find and replace?
It’s hard to tell exactly what you want because your refactoring example changes the import as well as the package, but the following will change
common. -> etc.common.for all files in a directory:This assumes you have gnu sed available, which most linux systems do. Also, just to let you know, this will fail if there are too many files for sed to handle at one time. In that case, you can do this:
Note that it might be a good idea to run the sed command as
sed -i'.OLD' 's/\bcommon\./etc.&/'so that you get a backup of the original file.