bash 递归搜索和替换文本的Unix命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20903988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unix command to search and replace text recursively
提问by Chaitanya
I am looking for a UNIX command that helps me to search for a text from all the files in a folder recursively and replace it with new value. After searching in internet I came across this command which worked for me.
我正在寻找一个 UNIX 命令,它可以帮助我从文件夹中的所有文件中递归搜索文本并将其替换为新值。在互联网上搜索后,我遇到了这个对我有用的命令。
find ./myFolder -type f -print0 | xargs -0 sed -i 's/Application/whatever/g'
找到 ./myFolder -type f -print0 | xargs -0 sed -i 's/Application/whatever/g'
Please help me in understanding the above command. I was not able to understand this part of the command : -print0 | xargs -0
, what this indicates? I know only basics in Unix so finding it difficulty in understanding this. I am using bash shell.
请帮助我理解上述命令。我无法理解命令的这一部分 : -print0 | xargs -0
,这表示什么?我只知道 Unix 的基础知识,因此很难理解这一点。我正在使用 bash shell。
Also are there any alternate commands that provides same functionality in Unix, from google searching I got commands related to Perl
scripting, I don't know Perl
so dropped the idea of using it.
还有在 Unix 中提供相同功能的任何替代命令,从谷歌搜索我得到了与Perl
脚本相关的命令,我不知道Perl
所以放弃了使用它的想法。
回答by anubhava
Also are there any alternate commands that provides same functionality in Unix
Also are there any alternate commands that provides same functionality in Unix
Yes you can do all this in find itself:
是的,您可以在 find 本身中完成所有这些:
find ./myFolder -type f -exec sed -i 's/Application/whatever/g' '{}' \;
-exec
option in find
is for:
-exec
选项 infind
用于:
-exec utility [argument ...] ;
True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon ('';''). If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a control operator. If the string ''{}'' appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file. Utility will be executed from the directory from which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs.
-exec utility [argument ...] ;
如果名为实用程序的程序返回零值作为其退出状态,则为真。可以将可选参数传递给实用程序。表达式必须以分号 ('';'') 结束。如果从 shell 调用 find,如果 shell 将分号视为控制运算符,则可能需要引用分号。如果字符串 ''{}'' 出现在实用程序名称或参数中的任何位置,则它将被当前文件的路径名替换。实用程序将从执行 find 的目录中执行。实用程序和参数不受 shell 模式和构造的进一步扩展的影响。
回答by NeronLeVelu
find
is passing through all file from a given path
-type f
limite to file (no folder, ...)
-print0
give the output to stdout with corresponding file
find
将所有文件从给定的路径
-type f
限制传递到文件(无文件夹,...)
-print0
将输出提供给带有相应文件的标准输出
so this give you all file from a starting point and all subfolder inside
所以这给你所有的文件从一个起点和里面的所有子文件夹
xargs
allow you to pass parameter to next command coming from previous one (so the file name here)
xargs
允许您将参数传递给来自前一个命令的下一个命令(所以这里的文件名)
sed -i
edit the input (here the passed file)
's/Application/whatever/g'
sed command that replace the pattern "Application" by "whatever", on any occurence (g
)
sed -i
编辑输入(这里是传递的文件)
's/Application/whatever/g'
sed 命令,在任何情况下,将模式“应用程序”替换为“任何”(g
)