Batch edit file content

method 1:
 
In the past two days, I have used maven to build an application. Because the project is very large, there are more than 700 pom.xml files. What is even more depressing is that many pom.xml files have separately specified the url of the resource library. I The URLs of these resource libraries need to be uniformly assigned to the nexus local central library.
It is not practical to manually change the file configuration one by one, so I googled it and found a good way to replace the contents of the files in batches. The command structure is as follows:
find -name 'To find The file name' | xargs perl -pi -e 's|replaced string|replaced string|g' The following example is to change the in all pom.xml files in the current directory and all subdirectories 8221; http://repo1.maven.org/maven2 “ replace with ” http://localhost:8081/nexus/content/groups/public
.find -name 'pom.xml ' | xargs perl -pi -e 's|http://repo1.maven.org/maven2|http://localhost:8081/nexus/content/groups/public|g' Perl language is used here,
perl -pi -e Add the -e option to a Perl command, followed by a line of code, and it will run the code like a normal Perl script.
Using Perl from the command line can help with some powerful, real-time transformations. Studying regular expressions carefully, and using them correctly, will save you a lot of manual editing.
find -name 'pom.xml' | xargs perl -pi -e 's|http://repo1.maven.org/maven2|http://localhost:8081/nexus/content/groups/public|g'
 
Method 2:

A simple way to batch replace strings in multiple files under Linux. Use the sed command to batch replace strings in multiple files.
Use the sed command to batch replace strings in multiple files. 
sed -i "s/original string/new string/g" `grep the directory where the original string -rl is located`
For example: I want to replace mahuinan with huinanma, execute the command: 
sed -i "s/mahuinan/huinanma/g " 'grep mahuinan -rl /www'
is currently the simplest batch replacement string command in linux!
The specific format is as follows: 
sed -i "s/oldString/newString/g" `grep oldString -rl /path`
Example code: sed -i "s/size/g" `grep size-rl /usr /aa`
sed -i "s/size/g" `grep size-rl ./`

Method 3:

During the development process of the schedule, you may encounter the situation of changing a variable name to another variable name. If the variable is a local variable, vi is sufficient, but if it is a global variable, and in the It is used in many files, and it is an unwise choice to use vi at this time. Here is a simple shell command that can modify the specified strings in all files at once:

grep "abc" * -R | awk -F: '{print $1}' | sort | uniq | xargs sed -i 's/abc/abcde/g'

 

 

Batch replace IP in configuration file:

grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" * -R | awk -F: '{print $1}' |  sort | uniq | xargs sed -i 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/172\.0\.0\.1/g' 


from:  http://blog.zol.com.cn/1395/article_1394052.htmlAdditional

note :
sed -i "s/oldString/newString/g" `grep oldString -rl /path`    
may not handle multiple files Support, you need to use  xargs , get.
The variant is as follows:
grepoldString -rl /path | xargssed -i "s/oldString/newString/g"

Note
in `grep oldString -rl /path` the ` is the quotation mark before 1` , not the  ' before enter

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326961948&siteId=291194637