0

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)

4
  • I don't think it's "ignoring [the] global flag" - it's just applying .* greedily Nov 23, 2018 at 13:26
  • @steeldriver ah that would make sense! Do you have any idea on how to rectify that? I am new to sed
    – wmash
    Nov 23, 2018 at 13:30
  • what exactly do you want the /.*/ to match? a single directory component, or something else?
    – thrig
    Nov 23, 2018 at 14:48
  • @thrig the /.*/ 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, 2018 at 14:52

1 Answer 1

0

Figured it out. Used find . -type f -name "*.js" -exec sed -i '' s,/[^]]*/marlon/express,/usr/src/app,g {} +. The parentheses stops it from greedily ripping through the rest of the line.

Thanks to this answer for steering me in the right direction: https://stackoverflow.com/questions/1685575/sed-regex-to-non-greedy-replace

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .