Home  >  Article  >  Operation and Maintenance  >  What is the linux replacement command?

What is the linux replacement command?

WBOY
WBOYOriginal
2022-01-27 14:28:2825651browse

Linux replacement command: 1. Use the ":s/original content/replacement content/" command in vim to replace the content; 2. Use sed with grep to replace the content; 3. Use "find -name file name | xargs perl -pi -e 's|Original content|Replacement content|g'" command replaces the content.

What is the linux replacement command?

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

What is the linux replacement command?

1. Replace through vim editor

You can use the :s command to replace strings in vi/vim .

:s/well/good/ Replace the first well in the current line with good

:s/well/good/g Replace all wells in the current line with good

: n,$s/well/good/ Replace the first well in each line from the nth line to the last line with good

:n,$s/well/good/g Replace the nth line to All wells in each line in the last line are good n is a number, if n is ., it means from the beginning of the current line to the last line

:%s/well/good/ (equivalent to: g/well/s/ /good/) Replace the first well in each line with good

:%s/well/good/g (equivalent to: g/well/s//good/g) Replace all wells in each line For good, you can use # as the separator. At this time, the / appearing in the middle will not be used as the separator

:s#well/#good/#. Replace the first well/ in the current line with good/

:%s#/usr/bin#/bin#g You can replace all paths /usr/bin in the file with /bin

2, cooperate with sed and grep

sed -i s/yyyy/xxxx/g `grep yyyy -rl --include="*.txt" ./`

Function: Replace the yyyy string in all txt files in the current directory (including subdirectories) with the xxxx string. Among them,

-i indicates that the file is being operated, and the grep command enclosed in `` indicates that the result of the grep command is used as the operating file.

s/yyyy/xxxx/ means to search for yyyy and replace it with xxxx, followed by g means that when there are multiple yyyy in a line, replace them all instead of just replacing the first one

In addition , if you do not need to find subdirectories and only need to replace in the current directory, just use the sed command. The command is as follows: sed -i s/xxxx/yyyy/g ./*.txt

3.find Command find and replace

find -name '要查找的文件名' | xargs perl -pi -e 's|被替换的字符串|替换后的字符串|g'

#Find and replace strings contained in the current directory and replace

find -name '*.txt' | xargs perl -pi -e 's| Replaced content|Replaced content|g'             #Recursive search and replacement

find . -type f -name '*.html' | xargs perl -pi -e 's|Replaced content|Replaced content|g'

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What is the linux replacement command?. 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