在几个文件中找到/替换的最佳方法? [英] Best way to do a find/replace in several files?
问题描述
我只想替换发生在一个文件夹和子-folders。最好的办法是什么?即时运行Ubuntu如果该事项。
-d
find。 -type f -print0 | xargs -0 -n 1 sed -i -e's / from / to / g'
第一部分是find命令来查找要更改的文件。您可能需要适当修改。 xargs命令会将找到的每个文件都找到并应用sed命令。 sed命令获取from的每个实例并将其替换为to。这是一个标准的正则表达式,所以可以根据需要进行修改。
如果您使用svn注意。您的.svn目录将被搜索并替换。你必须排除这些,例如:
find。 ! -regex。* [/] \.svn [/]?.*-type f -print0 | xargs -0 -n 1 sed -i -e's / from / to / g'
或
find。 -name .svn -prune -o -type f -print0 | xargs -0 -n 1 sed -i -e's / from / to / g'
what's the best way to do this? i'm no command line warrior, but i was thinking there's possibly a way using grep and cat.
i just want to replace a string that occurs in a folder and sub-folders. what's the best way to do this? i'm running ubuntu if that matters.
-d
find . -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'
The first part of that is a find command to find the files you want to change. You may need to modify that appropriately. The "xargs" command takes every file the find found and applies the "sed" command to it. The "sed" command takes every instance of "from" and replaces it with "to". That's a standard regular expression, so modify it as you need.
If you are using svn beware. Your .svn-directories will be search and replaced as well. You have to exclude those, e.g., like this:
find . ! -regex ".*[/]\.svn[/]?.*" -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'
or
find . -name .svn -prune -o -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'
这篇关于在几个文件中找到/替换的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!