Content in bulk replacement files under Linux

Source: Internet
Author: User

The grep command can find the contents of a single file or folder under a file. Use the parameter "R" or "R" to recursively find all the files under the specified folder; The parameter "L" lets the grep output the file name that contains the specified string, and immediately stops the query to the same file after the first match succeeds. If you do not use "l", grep queries a single file for all rows containing the specified string in the output file, and the folder query with the "file name: include string line" output.

With the knowledge mentioned above, the command to perform a bulk substitution can be written as:

grep "NETINGCN"-rl/assign-path | Xargs sed-i ' s/netingcn/www.netingcn/g '

Or

Sed-i ' s/netingcn/www.netingcn/g ' grep ' netingcn '-rl/assign-path '

There is also a more complicated way of writing:

grep "NETINGCN" *-r | Awk-f: ' {print $} ' | Sort | Uniq | Xargs sed-i ' s/netingcn/www.netingcn/g '

Where the command awk is used to split the string, uniq the result.

, information readily available on the web

Format: Sed-i "s/lookup field/replace field/g" ' grep lookup field-rl path '

Linux sed to bulk replace strings in multiple files

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

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 '

Exp:sed-i "s/shabi/$/g" ' grep shabi-rl./'

2. Additional additional

2.1 Replace the text "garden" in the file 1.txt with "Mirgarden"

# sed-i "s/garden/mirgarden/g" 1.txt//sed-i is simple

2.2 Replace "garden" in all files in the current directory with "Mirgarden"

# # Sed-i "s/garden/mirgarden/g" ' ls '/In fact, LS out of multiple file names just

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.