Linux Perl,sed,shell Bulk replacement of file content

Source: Internet
Author: User
Tags perl script

Method 1:

The two-day use of MAVEN in building an application, because project is very large, there are more than 700 pom.xml files, and even more depressing is that in many pom.xml files are individually specified in the repository URL, I need to assign the URLs of these repositories uniformly to the Nexus local Central library.

Manually change the file configuration is a bit less practical, so Google a bit, find the bulk of the replacement file content of a good way, the command structure is as follows:

Find-name ' file name to find ' | Xargs perl-pi-e ' s| replaced string | Replace String |g ' The following example is to replace "Pom.xml" in all http://www.111cn.net/phper/php.html files in the current directory and all subdirectories with " Http://www.111cn.net ".

Find-name ' Pom.xml ' | Xargs perl-pi-e ' S|http://www.111cn.net/phper/php.html|http://www.111cn.net|g ' uses the Perl language here,

Perl-pi-e adds the-e option to the Perl command followed by a line of code that runs the code like a normal Perl script.

Using Perl from the command line can help achieve some powerful, real-time transformations. Seriously studying regular expressions and using them correctly will save you a lot of manual editing work.

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 bulk replace strings in multiple files under Linux. Use the SED command to bulk replace strings in multiple files.

Use the SED command to bulk replace strings in multiple files.

Sed-i "s/original string/new string/g" ' grep original string-rl directory '

For example: I'm going to replace Mahuinan with Huinanma and execute the command:

Sed-i "s/mahuinan/huinanma/g" ' grep mahuinan-rl/www '

This is the current Linux simplest batch replacement string command!

The specific format is as follows:

Sed-i "s/oldstring/newstring/g" ' grep oldstring-rl/path '

Instance code: Sed-i "s/size/Moon fire/g" ' grep size how much-RL/USR/AA '

Sed-i "s/size/Moon fire/g" ' grep size how much-rl./'

Method 3:

During the development of the schedule, perhaps you will encounter a variable name modified to another variable name, if this variable is a local variable, vi sufficient, but if it is a global variable, and in many files are used, this time using VI is an unwise choice. Here is a simple shell command to modify the specified string in all files at once:

grep "ABC" *-r | Awk-f: ' {print $} ' | Sort | Uniq | Xargs sed-i ' s/abc/abcde/g '

Supplementary Note:

Sed-i "s/oldstring/newstring/g" ' grep oldstring-rl/path '

Processing of multiple files may not be supported and needs to be done with Xargs.

Variants are as follows:

grep Oldstring-rl/path | Xargs sed-i "S/oldstring/newstring/g"

Attention:

In ' grep oldstring-rl/path ', ' the inverted quote in front of 1 ', not the '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.