Linux batch file replacement methods (Perl, sed, shell)

Source: Internet
Author: User
Tags perl script

Original article link

Linux Perl shell Perl VI grep awk xargs batch replacement

Method 1:   Maven is used to build an application in the past two days. Due to the large project, there are more than 700 pom S. XML file, even more depressing is in many pom. the URL of the resource library is specified separately in the XML file. I need to uniformly specify the URLs of these resource libraries to the local central nexus library.
Manually changing the file configuration is a bit impractical, So Google, find a good way to replace the 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' the following example shows the current directory and all all pom. & #8221; Http://repo1.maven.org/maven2& #8220; Replace with & #8221; Http: // localhost: 8081/nexus/content/groups/Public& Amp; #8220 ;.
Find-name 'pom. XML '| xargs Perl-pi-E's | http://repo1.maven.org/maven2 | http: // localhost: 8081/nexus/content/groups/Public | G' the Perl language is used here,
Add the-e option to the Perl Command, followed by a lineCodeThen it will run the code as if it were running a common Perl script.
Using Perl from the command line can help implement some powerful and real-time conversions. Studying Regular Expressions carefully 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 method to replace strings in multiple files in batches in Linux. You can use the SED command to replace strings in multiple files in batches.
You can use the SED command to replace strings in multiple files in batches.
Sed-I "s/original string/New String/g" 'grep original string-rl directory'
For example, to replace mahuinan with huinanma, run the following command:
Sed-I "s/mahuinan/huinanma/g" 'grep mahuinan-RL/WWW'
This is the simplest string replacement command in Linux!
The specific format is as follows:
Sed-I "s/oldstring/newstring/g" 'grep oldstring-RL/Path'
Instance code: sed-I "s/size/Sun/month/g" 'grep size-RL/usr/'
Sed-I "s/size/Sun/month/g" 'grep size-rl ./'

Method 3:

During the development of the schedule, you may encounter the situation of changing a variable name to another variable name. If this variable is a local variable, VI is sufficient, however, if a global variable is used in many files, using VI is an unwise choice. A simple shell command is provided here to modify the specified string in all files at a time:
Grep "ABC" *-r | awk-F: '{print $1}' | sort | uniq | xargs sed-I's/ABC/ABCDE/G'

From: http://blog.zol.com.cn/1395/article_1394052.html

Additional instructions:
Sed-I "s/oldstring/newstring/g" 'grep oldstring-RL/Path'
Processing of multiple files may not be supported.Xargs.
The variants are as follows:
GrepOldstring-RL/path | xargsSed-I "s/oldstring/newstring/G"

note :
In 'grep oldstring-RL/path' ' is quotation marks before 1 ' , and not before enter '

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.