Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of finding and replacing the contents of Linux files?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the method of finding and replacing the contents of Linux files". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method of finding and replacing the contents of Linux files?"

1. Replace through the vim editor

The: s command can be used in vi/vim to replace strings. : s/well/good/ replaces the current line with the first well for good: s/well/good/g replaces all well for the current line for good: n well for the beginning of the nth line to the last line of the replacement, the first well for each line from the beginning of the nth line to the last line of the replacement is good: n for the beginning of the nth line to the last line, if n is the number. Indicates that from the current line to the last line:% s/well/good/ (equivalent to: g/well/s//good/) replace the first well of each line with good:% s/well/good/g (equivalent to: g/well/s//good/g) replace all well in each line to good can use # as the delimiter The / that appears in the middle will not be used as a delimiter: s#well/#good/# replaces the current line with the first well/ good/:% s#/usr/bin#/bin#g can replace all paths / usr/bin in the file with / bin 12345678910111213141516172, sed and grep

Sed-I s/yyyy/xxxx/g `grep yyyy-rl-- include= "* .txt". / `function: replace yyyy strings in all txt files in the current directory (including subdirectories) with xxxx strings. Where-I means the operation is a file, and the grep command enclosed by ``means that the result of the grep command is taken as the operation file. S/yyyy/xxxx/ means to find yyyy and replace it with xxxx, followed by g means that when there are multiple yyyy in a line, replace them, not just the first one. If you do not need to find subdirectories, you only need to replace them in the current directory, using the sed command as follows: sed-I s/xxxx/yyyy/g. / *. Txt 1234567893.find command to find and replace

Find-name 'File name to find' | xargs perl-pi-e's | string to be replaced | string to be replaced | g'# find and replace the string contained in the current directory and replace find-pi'* .txt'| xargs perl-pi-e's | replace content | replace content | g'# recursively find replacement find. -type f-name'* .html'| xargs perl-pi-e's | replaced content | replaced content | G' so far, I believe you have a better understanding of "what is the method of finding and replacing Linux files". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report