本文共 570 字,大约阅读时间需要 1 分钟。
没啥多说的!下边这条命令就是搜索当前目录下的所有文件,并将文件里边包含的所有 world 替换成 hello。
注意事项:包含 grep 命令的是反引号 ` (反引号,反引号,反引号,重要的事情说三遍,如果不知道反引号是什么,可以去 Google 一下)而不是单引号 '。
sed -i "s/world/hello/g" `grep -rl world ./`
-i 参数表示直接在原文件中替换,不添加的话只是把替换的结果在命令行显示出来,原文件并不会真正被修改。
示例:
[root@master shell_learning]# grep -r "world" ./ ./test.rb: Hello world ./test1.txt:world./test2.rb: return 'world' if item[root@master shell_learning]# sed -i "s/world/hello/g" `grep -rl world ./`[root@master shell_learning]# grep -r "hello" ./ ./test.rb: Hello hello ./test1.txt:hello./test2.rb: return 'hello' if item
转载地址:http://vjjqi.baihongyu.com/