Linux

如何使用 sed 替換字元串?

  • October 13, 2021

我正在嘗試sed用另一個字元串替換一個字元串。我使用了下面的命令,它工作得很好,直到我跑了cat index.html,它又回到了舊的字元串。有人可以幫忙嗎?

<h1>
Hello World
</h1>
<p>This is out first WebDev study</p>
</body>
</html>
$ sed 's/Hello World/About Us/' index.html
<!DOCTYPE>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>
About Us
</h1>
<p>This is out first WebDav study</p>
</body>
</html>
$ cat index.html
<!DOCTYPE>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>
Hello World
</h1>
<p>This is out first WebDav study</p>
</body>
</html>

從手冊頁:

-i[SUFFIX], --in-place[=SUFFIX]
    edit files in place (makes backup if SUFFIX is supplied)

因此,在您的情況下,您只需要添加標誌:

sed -i 's/Hello World/About Us/' index.html

引用自:https://unix.stackexchange.com/questions/664319