我在理解如何存储文件以sed修改时遇到问题。
grep -Fr "from 'app" --include=*.js src/ | sed -s "s:from 'app: from ':"
我已经看到在一个已知文件上使用带有sed的grep来存储到变量中。
$file = file.txt
grep "import from {} redux" $file | sed -s "s/ReactJS/ReactJS./"
$ file`
如何在src中处理多个文件* .js?
答案 0 :(得分:2)
您需要在-l
中添加grep
选项以输出匹配的文件名,然后将ut传递到xargs
以处理每个匹配的文件名:
grep -ZlrF "from 'app" --include=*.js | xargs -0 sed "s:from 'app: from ':"
在-Z
中使用了grep
选项,以使用可以使用xargs -0
使用的NUL字符来分隔输出。这用于处理带有空格和其他特殊字符的文件名。
如果您没有gnu grep
,请按照Ed的建议使用find + sed
:
find . -name '*.js' -print0 | xargs -0 sed "s:from 'app: from ':"