bash Shell Script:如何在文件夹内递归查找和替换句子(不是单词)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12098704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:06:28  来源:igfitidea点击:

Shell Script: How to Find and Replace the sentence (not a word) recursively inside a folder

bashshell

提问by SridharKritha

Q. Consider a sample file.txt:

问:考虑一个示例文件.txt:

World Hello world Hello Hell Hello Hello world

世界你好你好你好你好世界

How to find and replace the sentence "Hello World" into "Hai Universe"

如何找到句子“Hello World”并将其替换为“Hai Universe”

World Hai Universe Hello Hell Hello Hai Universe

世界海宇宙你好地狱你好海宇宙

I'd like to this changes recursively in all the matching files inside a folder.

我想在文件夹内的所有匹配文件中递归更改此更改。

Advance Thanks.

提前谢谢。

回答by newfurniturey

Try using findwith sed:

尝试使用findsed

find . -type f -exec sed -i 's|Hello World|Hai Universe|g' {} \;

This will find all of the files, recursively, in the current directly and then execute a sedfind+replace on each file individually.

这将直接递归地查找当前中的所有文件,然后分别sed对每个文件执行查找+替换。

回答by karthik

You can try something like this, using sedcommand in Unix.

你可以尝试这样的事情,sed在 Unix 中使用命令。

sed -i 's/Hello World/Hai Universe/g' filename