Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to replace the contents of files in batch under Linux system

Shulou Source: shulou.com Published: 2022-06-01 03:58:11 09月15日 Update

How to replace file content in batches under Linux system, in response to this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem find a simpler and easier way.

In the past two days, I used maven to build an application. Because the project is very large, there are more than 700 pom.xml files. What is more frustrating is that many pom.xml files specify the url of the resource library separately. I need to specify the url of these resource libraries to the nexus local central library.

Changing file configurations manually one by one is a bit impractical, so I did a little google and found a good way to replace file contents in batches. The command structure is as follows:

find -name 'file name to find'| xargs perl -pi -e 's| Substituted string| Substituted string| g'

The following example replaces "http://repo1.maven.org/maven2" with "http://localhost:8081/nexus/content/groups/public" in all pom.xml files in the current directory and all subdirectories.

find -name 'pom.xml' | xargs perl -pi -e 's|http://repo1.maven.org/maven2|http://localhost:8081/nexus/content/groups/public|g'

Here's Perl,

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.

About Linux system how to batch replace the file content of the answer to the problem shared here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Tags: File content command question help system code character string manual method more resource library solution run powerful large easy ordinary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno NVidia Shulou Tech Info macOS