Replacing the content in files in batches in Centos

Source: Internet
Author: User

The sed command is easy to use for a single file. If you want to replace all the files in a specified folder, combine grep and sed into a command. The grep command can be used to search for the content of a single file or a file in a folder. The parameter "r" or "-R" can be used to Recursively search for all files in a specified folder; the "l" parameter allows grep to output the file name containing the specified string. After the first successful match, the query on the same file is stopped immediately. If you do not use "l" or "grep" to query a single file, all rows in the output file containing the specified string are output in the format of "File Name: Contains string rows" When querying the folder.

With the knowledge mentioned above, you can write the commands for batch replacement as follows:

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 complicated writing method:

Grep "netingcn" *-R | awk-F: '{print $1}' | sort | uniq | xargs sed-I's/netingcn/www \. netingcn/G'

The command awk is used to split the string and uniq to deduplicate the result.


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.