2012-01-17 69 views
2

我想通過bash控制檯搜索並替換位於幾個文件中的字符串。在控制檯中搜索並替換

下面是我用它來找到字符串的文件的命令:

grep "string" * -r

所以上面的是搜索,現在我需要一個命令替換string

這可能嗎?

+1

當然,你有沒有試過'sed'? – 2012-01-17 22:07:06

+1

請參閱http://stackoverflow.com/questions/4996460/how-do-i-replace-the-word-hello-with-goodbye-in-every-file-in-this-directory/4996483 – Antti 2012-01-17 22:09:44

回答

4

http://www.grymoire.com/Unix/Sed.html

這是胡思亂想和困難,但它是如何做這件事。

下面是一個例子:

sed -i 's/ugly/beautiful/g' /home/bruno/old-friends/sue.txt 

這取代醜陋與美麗sue.txt。

+0

在OS X上失敗(FreeBSD),命令a預期\後面跟着文本。 – 2014-02-26 21:53:14

+0

你有一個錯字,聽起來像。在我的例子中沒有命令「a」。 – Almo 2014-02-26 21:54:18

+0

你需要在Mac/FreeBSD上提供備份擴展,例如'sed -i .bk's/e/d/g'myfile.txt' – 2014-02-27 07:55:06