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 File contents in batches by linux

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how linux replaces file contents in batches". In daily operation, I believe many people have doubts about how linux replaces file contents in batches. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how linux replaces file contents in batches". Next, please follow the editor to study!

In linux, you can replace file contents in batches with the sed command, which uses scripts to process text files and replaces all matching strings within the file with the syntax of "sed-I" s / old content / new content / g "file path".

The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to replace the contents of files in batch by linux

1. Replace the string that matches all files in the folder

Sed-I "s / old content / new content / g" `grep old content-rl folder path`

For example, change the abc string in the contents of all files under the / var/www/test folder to 123

Sed-I "s/abc/123/g" `grep abc-rl / var/www/ test`

Note: grep content is not in single quotation marks, but in `, above the tab key, please use the absolute path

two。 Replace all matching strings in the file

Sed-I "s / old content / new content / g" file path

For example, change the abc string in the contents of all files under the / var/www/test/index.html folder to 123

Sed-I "s/abc/123/g" / var/www/test/index.html

Linux batch replaces the specified contents of all files in the specified folder

Command: sed-I "s#https#http#g" `grep http-rl Veromoda`

Function: used to replace the http in all files in the current directory VEROMODA folder and subfolders.

The Linux sed command uses scripts to process text files.

Sed can process and edit text files according to the instructions of the script.

Sed is mainly used to edit one or more files automatically, simplify the repeated operation of files, write conversion programs, and so on.

Grammar

Sed [- hnV] [- e] [- f] [text file]

Parameter description:

-e or-- expression= processes the input text file with the script specified in the option.

-f or-- file= processes the input text file with the script file specified in the option.

-h or-- help displays help.

-n or-- quiet or-- silent displays only the results after script processing.

-V or-- version displays version information.

Action description:

A: added, a can be followed by strings, and these strings will appear on the new line (the current next line) ~

C: instead, c can be followed by strings, which can replace the lines between N1 and N2!

D: delete, because it is a deletion, so he Dongdong is not usually replaced by d.

I: insert, I can be followed by strings, and these strings will appear on the new line (the current previous line)

P: print, that is, the selected data is printed out. Usually p runs with the parameter sed-n ~

S: replace, you can directly carry out the work of replacement! Usually the action of this s can be matched with the formal representation! For example, 1Magazine 20s Universe, OldCompact newscop g is it!

At this point, the study on "how to replace the contents of files in batches by linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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