sed 对于每行多次出现不正确 - 忽略全局标志

sed 对于每行多次出现不正确 - 忽略全局标志

迭代具有特定扩展名的文件并更改所有出现的子字符串sed。有时每行会出现多次。这是命令:

find . -type f -name "*.js" -exec sed -i '' s,/.*/marlon/express,/usr/src/app,g {} +

遍历js文件以将所有出现的通配子字符串替换为/usr/src/app.

这对于单次出现的线路效果很好,但对于多次出现的线路似乎会搞砸。结果变成了这样:

__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['/usr/src/app/client/js/manifest.js","s":{},"b":{},"f":{},"fnMap":{},"statementMap":{},"branchMap":{}};

该命令似乎删除了'] = {"path":"/Users/willashworth/Documents/Pimberly/marlon/express/client/js/manifest.js该文件中第二次出现的所有内容。

整个文件是:

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'];

问题出在第 5 行

编辑

我根据这个答案构建了这个命令:find & sed(搜索和替换)

答案1

弄清楚了。用过的find . -type f -name "*.js" -exec sed -i '' s,/[^]]*/marlon/express,/usr/src/app,g {} +。括号阻止它贪婪地撕毁该行的其余部分。

感谢这个答案引导我走向正确的方向:https://stackoverflow.com/questions/1685575/sed-regex-to-non-greedy-replace

相关内容