Shell scripts replace file names and contents of files in bulk

Source: Internet
Author: User

Method 1:

These two days in the camera Bringup, need to add chromatic file, because there is too much to replace the contents of the file, manually change the file configuration a bit less practical, so Google a bit, to find a good way to replace the contents of the bulk of the file, command structure as follows:

grep "substituted string" *-r | Awk-f: ' {print $} ' | Sort | Uniq |xargs sed-i ' s/replaced string/replaced string/'

For example: if we need to replace 250 of all the files in the current directory with 38, then the command is:

grep "+" *-r | Awk-f: ' {print $} ' | Sort | Uniq | Xargs sed-i ' s/250/38/g '

If you want to replace the file name, understand the above method is very simple, we only need a little modification to achieve

For example, change the name of the current directory to include 250 to 38, and the command is:

Find-name *250*| Awk-f: ' {print $} ' | Sort | Uniq | Xargs rename ' s/250/38/'

In the course of the development of the schedule, you may encounter the case of modifying 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 used in many files, this time using VI is an unwise choice. Here is a simple shell command that allows you 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 '

If you need to change the file name in bulk, you can perform

Find-name *adc*| Awk-f: ' {print $} ' | Sort | Uniq | Xargs rename ' s/abc/abcde/g '

To bulk replace IP in a configuration file:

grep "[0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\} "*-r |  Awk-f: ' {print $} ' | 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 '

Additional Notes :

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 want to replace Mahuinan with Huinanma and execute the command:
Sed-i "s/mahuinan/huinanma/g" ' grep mahuinan-rl/www '
This is the most simple batch replacement string command for Linux at the moment!

The specific format is as follows:

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

The processing of multiple files may not be supported, need to use xargs, fix.
Variants are as follows:

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

Note :
In ' grep oldstring-rl/path ' for ' 1 front ', instead of ' before enter '


This article is from the "canvas shoes can walk the cat step" blog, please be sure to keep this source http://9409270.blog.51cto.com/9399270/1981005

Shell scripts replace file names and contents of files in bulk

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.