如何grep和替换 [英] How to grep and replace

查看:123
本文介绍了如何grep和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to recursively search for a specified string within all files and subdirectories within a directory and replace this string with another string.

我知道要查找的命令它可能看起来像这样:

I know that the command to find it might look like this:

grep 'string_to_find' -r ./*

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

But how can I replace every instance of string_to_find with another string?

推荐答案

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

Another option is to use find and then pass it through sed.

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

这篇关于如何grep和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆