Home  >  Article  >  Operation and Maintenance  >  How to replace string in linux

How to replace string in linux

WBOY
WBOYOriginal
2021-12-31 14:21:2517461browse

In Linux, you can use the sed command to replace a string. This command is mainly used to automatically edit one or more files. When the parameter is set to "s", the content can be replaced. The syntax is "sed ' s/original string/new string/'file".

How to replace string in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to replace a string in Linux

If you want to replace a string in Linux, you can use the sed command.

The syntax is as follows:

sed 's/原字符串/新字符串/' 文件
sed 's/原字符串/新字符串/g' 文件

The difference between these two command formats is whether there is a "g". Without "g", it means to replace only the first matching string. With "g", it means to replace all matching strings. "g" can be considered as the abbreviation of "global". There is no "global" "Don't replace everything at the end, it will be easier to remember.

Commands can be processed using regular expressions. As shown in the figure below, the first command only replaces the first "b" with "B" and does not replace the others; while the second command replaces all numbers in the entire text with "ok".

Examples are as follows:

How to replace string in linux

How to replace string in linux

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of How to replace string in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn