linux - 如何使用 grep 进行搜索和替换

标签 linux shell replace grep

我需要在一个目录的所有文件和子目录中递归搜索一个指定的字符串,并用另一个字符串替换这个字符串。

我知道找到它的命令可能如下所示:

grep 'string_to_find' -r ./*

但是如何将 string_to_find 的每个实例替换为另一个字符串?

最佳答案

另一种选择是使用 find 然后通过 sed 传递它。

find /path/to/files -type f -exec sed -i 's/oldstring/new string/g' {} \;

关于linux - 如何使用 grep 进行搜索和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15402770/

相关文章:

linux - shell 脚本 : how to output to command line?

python - Paramiko 在 psql 上执行命令

c# - 替换c#中字符串的最后一个字符

Java : replacing text URL with clickable HTML link

regex - 修剪目录中所有文件的一部分文件扩展名 - Linux

c - epoll epollin 读取数据

Linux,子进程超时

linux - 根据具有有限列的第二个列表的内容编辑一个列表的内容

python - 是否可以从代码进入 ipython?

java - StringBuilder - 完全替换现有字符串。有什么方法吗?