Iterating through files with a specific extension and changing all occurrences of a substring with sed
. Sometimes there are multiple occurrences per line. This is the command:
find . -type f -name "*.js" -exec sed -i '' s,/.*/marlon/express,/usr/src/app,g {} +
Iterating through js
files to replace all occurrences of that wildcareded substring with /usr/src/app
.
This works great on lines with a single occurrence but with multiple it seems to mess it up. It turned this:
__cov_cMGKPxliNj_ByZm9tnD9jQ['/Users/willashworth/Documents/Pimberly/marlon/express/client/js/manifest.js'] = {"path":"/Users/willashworth/Documents/Pimberly/marlon/express/client/js/manifest.js","s":{},"b":{},"f":{},"fnMap":{},"statementMap":{},"branchMap":{}};
...to this:
__cov_cMGKPxliNj_ByZm9tnD9jQ['/usr/src/app/client/js/manifest.js","s":{},"b":{},"f":{},"fnMap":{},"statementMap":{},"branchMap":{}};
The command seemed to remove everything from '] = {"path":"/Users/willashworth/Documents/Pimberly/marlon/express/client/js/manifest.js
which is the 2nd occurrence in that file.
The whole file is:
var __cov_cMGKPxliNj_ByZm9tnD9jQ = (Function('return this'))();
if (!__cov_cMGKPxliNj_ByZm9tnD9jQ.__coverage__) { __cov_cMGKPxliNj_ByZm9tnD9jQ.__coverage__ = {}; }
__cov_cMGKPxliNj_ByZm9tnD9jQ = __cov_cMGKPxliNj_ByZm9tnD9jQ.__coverage__;
if (!(__cov_cMGKPxliNj_ByZm9tnD9jQ['/Users/willashworth/Documents/Pimberly/marlon/express/client/js/manifest.js'])) {
__cov_cMGKPxliNj_ByZm9tnD9jQ['/Users/willashworth/Documents/Pimberly/marlon/express/client/js/manifest.js'] = {"path":"/Users/willashworth/Documents/Pimberly/marlon/express/client/js/manifest.js","s":{},"b":{},"f":{},"fnMap":{},"statementMap":{},"branchMap":{}};
}
__cov_cMGKPxliNj_ByZm9tnD9jQ = __cov_cMGKPxliNj_ByZm9tnD9jQ['/Users/willashworth/Documents/Pimberly/marlon/express/client/js/manifest.js'];
With the issue being on line 5
EDIT
I have built this command from this answer: find & sed (search and replace)
.*
greedily – steeldriver Nov 23 '18 at 13:26sed
– wmash Nov 23 '18 at 13:30/.*/
to match? a single directory component, or something else? – thrig Nov 23 '18 at 14:48/.*/
should match/Users/willashworth/Documents/Pimberly/
. I am using a wildcard though instead of hardcoding as different users will have a different filepath but the/marlon/express
will remain constant which is why I am trying to use that as a terminator to stop the greedy operator ripping through the rest of the line – wmash Nov 23 '18 at 14:52