Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to replace the contents of files in batch under linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to achieve batch replacement of file content under linux", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "how to achieve batch replacement of file content under linux"!

For a single file, it is easy to do this using the sed command. If you need to replace all the files in a specified folder, it can also be easily done by combining grep and sed into a single command.

The grep command can find the contents of a single file or folder, use the parameter "r" or "- R" to recursively find all the files under the specified folder; the parameter "l" allows grep to output the file name containing the specified string, and immediately stop querying the same file after the first successful match. If you do not use "l", when grep queries a single file, all lines in the output file that contain the specified string are output as "File name: containing character Serial" when querying for folders.

Replace the following.

Http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js

Http://libs.baidu.com/jquery/1.9.0/jquery.js

The code is as follows:

Sed-I "s/ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js/libs.baidu.com/jquery/1.9.0/jquery.js/g" 1.htm

Sed-I "s/ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js/libs.baidu.com/jquery/1.9.0/jquery.js/g" `ls`

You may be interested in the use of sed for linux.

With the knowledge mentioned above, the command to perform a batch replacement can be written as follows:

Grep "netingcn"-rl / assign-path | xargs sed-I's Universe netingcnUnix www.netingcnUnix g'

Or

Sed-I's Universe netingcn netingcn www.netingcnAccording to g '``grep "netingcn"-rl / assign- path`

There is also a more complicated way to write it:

Grep "netingcn" *-R | awk-F:'{print $1}'| sort | uniq | xargs sed-I's Unix netingcnUnix www.netingcnhand g'

The command awk is used to split the string and uniq to remove the weight of the result.

1. Ready-made materials on the Internet

Format: sed-I "s / lookup field / replacement field / g" `grep lookup field-rl path`

Linux sed batch replaces strings in multiple files

Sed-I "s/oldstring/newstring/g" `grep oldstring-rl yourdir`

For example: replace www.admin99.net in all files under / home with admin99.net

Sed-I "s/www.admin99.net/admin99.net/g" `grep www.admin99.net-rl / Home`

Exp:sed-I "s/shabi/$/g" `grep shabi-rl. / `

2. Add extra to yourself.

2.1 replace the word "garden" in the file 1.txt with "mirGarden"

# sed-I "s/garden/mirGarden/g" 1.txt / / sed-I is very simple

2.2 replace "garden" with "mirGarden" in all files in the current directory

# # sed-I "s/garden/mirGarden/g" `ls` / / means that ls has multiple file names

Thank you for your reading, the above is the content of "how to achieve batch replacement of file content under linux". After the study of this article, I believe you have a deeper understanding of how to achieve batch replacement of file content under linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report