bash - 如何从命令行进行递归查找和替换?

标签 bash shell readline

是否有一种简单的方法可以递归搜索目录层次结构中的所有文件以查找术语(例如 port)并将所有出现的该术语替换为另一个(例如 port-lookup).

我已经尝试了 this post 中的建议, 但它们仅适用于单个文件。

最佳答案

实现此目的的最简单(并且可以说是最纯粹和直观的)方法是执行递归文件 grep 然后将这些文件结果通过管道传输到 sed(通过 xargs)以处理就地替换。

grep -r -l "port" | xargs -r -d'\n' sed -i 's/port/port-lookup/g'

There is a comprehensive community wiki answer on this topic that I would recommend reading over on the Unix & Linux exchange.

关于bash - 如何从命令行进行递归查找和替换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46482569/

相关文章:

python - Windows 上的 Pyreadline 任意自动完成

linux - 使用 Gksudo 运行 sudo 等脚本

bash - 根据月份名称创建目录

正则表达式匹配 bash 中文件的全部内容

shell - 在 FreeBSD 中为 shell 脚本创建启动守护进程

shell - 简单的 shell 脚本可以在 cygwin 中运行,但不能在 solaris 上运行

readline - 使用 GNU Readline;如何在同一程序中添加 ncurses?

linux - Awk/sed Loop over CSV - 将 child 绑定(bind)到第 1 列

java - 在 UNIX 中通过 crontab 安排作业

c - Readline 和 NCurses,处理特殊键和组合键