Linux Perl shell Perl VI grep awk xargs batch replacement

Source: Internet
Author: User
Tags perl script
Method 1:   Maven is used to build an application in the past two days, there are more than 700 pom. 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.
it is not practical to manually change the file configurations one by one, So Google will 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 POM in all subdirectories. replace & #8221; http://repo1.maven.org/maven2 & #8220; with & #8221; htT P: // localhost: 8081/nexus/content/groups/Public & #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-pi-e command, followed by a line of Code , then it will run the code like a normal 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
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:
Grep Oldstring-RL/path | xargs Sed
-I "s/oldstring/newstring/G"

Note::
In 'grep oldstring-RL/path' Medium ` Is Before 1 Quotation marks of Edges ` , And Not enter
Before Of '

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.