BASH:递归程序来替换文本文件中的树 [英] BASH: recursive program to replace text in a tree of files

查看:78
本文介绍了BASH:递归程序来替换文本文件中的树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在打击全新的,但我似乎无法找到一个办法让它做我想做的。

I am completely new at Bash but I just can't seem to find a way to make it do what I want.

假设你有一个树形目录与2个文件:
/top.php和/test/bottom.php

Imagine you have a tree directory with 2 files: /top.php and /test/bottom.php

如何让我的函数查找和替换说你好到卓悦,在/top.php和/test/bottom.php?

How do I make my function look and replace say "hello" into "bonjour" in /top.php AND in /test/bottom.php?

到目前为止,我发现这样做,这是一个在不同深度级别调用同一个函数两次的唯一方式:

So far the only way I have found to do this is by calling the same function twice with a different depth level:

find ./*.php -type f -exec sed -i 's/hello/bonjour/' {} \;
find ./*/*.php -type f -exec sed -i 's/hello/bonjour/' {} \;

肯定有一个线做这个递归的方式?

Surely there's a recursive way to do this in one line?

推荐答案

使用一个实际的模式来发现,而不是外壳通配符扩展:

Use an actual pattern for find instead of shell wildcard expansion:

find . -name '*.php' -type f -exec sed -i 's/hello/bonjour/' {} \;

这篇关于BASH:递归程序来替换文本文件中的树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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