Replacing File content in Centos in batches
These two Angels used the locomotive to collect some data because they generated a lot of SQL files. I want to replace the fixed content in each SQL file with another content.
Manual file configuration change is a bit impractical, so google, find a good way to replace file content in batches.
The command structure is as follows:
Find-name 'file name to be searched' | xargs perl-pi-e's | replaced string | G'
In the following example, replace "love margin" with "http://www.111cn.net" in all the SQL files under the current directory and all subdirectories"
Find-name *. SQL | xargs perl-pi-e's | xiangxiangyuan | http://www.111cn.net | G'
The Perl language is used here,
Perl-pi-e
Add the-e option to the Perl command, followed by a line of code, and then it will run the code like running a common Perl script.
Using Perl from the command line can help implement some powerful and real-time conversions. Carefully studying regular expressions and using them correctly will save you a lot of manual editing work.
Example
Change the content of all conf files under/usr/local/apache/conf to/data/wwwroot ]. The procedure is as follows:
# Sed-I "s/\/usr \/local \/apache \/htdocs/\/data \/wwwroot/g" 'find. -name "*. conf "| xargs grep"/usr/local/apache/htdocs "-rl'
Or
# Find.-type f-exec sed-I's #/usr/local/apache/htdocs #/data/wwwroot # G '{}\;