如何用另一个字符串替换文本文件中指定字符串的所有实例?

Ste*_*eod 1 command-line-interface text mac-osx search-and-replace

我有一个文件 a.txt。我想用该文件中的“1.5”替换“1.6”的所有实例,覆盖原始文件。

slu*_*man 11

使用命令行:

sed -i .bak -e 's/1\.5/1.6/g' /path/to/file
Run Code Online (Sandbox Code Playgroud)

此命令替换文件中,原始文件保存为/path/to/file.bak


Ali*_*ani 7

您可以使用 sed :


sed 's/1\.5/1\.6/g' /path/to/file | tee output
Run Code Online (Sandbox Code Playgroud) 此外,如果您在像 vim 这样的编辑器中,您可以这样做:

vim /path/to/file
:%s/1\.5/1\.6/g 
Run Code Online (Sandbox Code Playgroud) 在 emacs 中:

emacs /path/to/file
M-x replace-string
Run Code Online (Sandbox Code Playgroud)