Linux sed multiple file bulk replacement strings

Source: Internet
Author: User

The company moved, several server IP also changed, so the server OTA application of a lot of files related to IP replaced by the current IP, this time you can use the SED command. To bulk find replacements in a directory:

Sed is an online editor that handles a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer, known as pattern spaces, followed by the SED command to handle the contents of the buffer, and the contents of the buffer are sent to the screen after processing is complete. The next line is processed so that it repeats until the end of the file. The contents of the file do not change unless you use redirected storage output. SED is used to automatically edit one or more files, to simplify the repetitive operation of files, to write conversion programs, and so on.

[Root@www ~]# sed [-NEFR] [action]
Options and Parameters:
-N: Use Quiet (silent) mode. In general sed usage, all data from STDIN is generally listed on the terminal. However, if you add the-n parameter, only the line (or action) that is specifically handled by SED will be listed.
-E: SED action edit directly on command line mode;
-F: Directly write the action of SED in a file,-f filename can run the SED action within filename;
-r:sed's actions support the syntax of extended formal notation. (Default is the basic formal representation of French law)
-I: Directly modify the contents of the read file, rather than output to the terminal.

Action Description: [n1[,n2]]function
N1, N2: Does not necessarily exist, generally represents "select the line of action", for example, if my action needs to be between 10 and 20 lines, then "10,20[Action Behavior]"

function
A: New, a can be followed by a string, and these strings will appear on the new line (the current line) ~
C: Replace, C can be followed by strings, these strings can replace the line between N1,N2!
D: Delete, because it is deleted ah, so d usually do not pick up any of the drums;
I: Insert, I can be followed by strings, and these strings will appear on a new line (the current line);
P: Print, that is, to print out a selected data. Normally p will run along with the parameter Sed-n ~
S: Replace and can be directly substituted for work miles! Usually this s action can be paired with formal notation! For example, 1,20s/old/new/g is!

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

For example, replace all IP address 10.168.195.52 in the/product directory with 172.27.77.72, so you can:

Sed-i "s/10.168.195.52/172.27.77.72/g" ' grep 10.168.195.52-rl/product '

For example: Replace www.111cn.net in all files under/home as 111cn.net

Sed-i "s/www.111cn.net/111cn.net/g" ' grep www.111cn.net-rl/home '

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./'

The ' symbol is the TAB key, the number 1 on the left, not the single quote '

If all the files that are pending are under a single folder, you can write this.

If the file that is being processed is under more than one folder, this will cause an error because SED cannot process the folder and must add "GREP-RL" so that you can skip the folder and list the files directly.

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.