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

What's the use of sed?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the use of sed, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Pipeline command sed is a good text processing tool, which is mainly processed by behavior units, and can replace, delete, add, select and so on. Sed can handle standard input content or files, and can output standard output or output to files. The general rules for using sed are as follows:

Sed-nefri 'Action Command' file or input

Parameter settings:

-n: use silent mode. In the general use of sed, all data from STDIN is generally listed on the screen. But if you add the-n argument, only the row (or action) that has been specially handled by sed will be listed.

-e: edit sed actions directly on the command line

-f: directly write the action commands of sed in a file, and-ffilename can perform sed actions in filename.

The-r:sed action supports the syntax of extending the regular representation (the default basic regular representation syntax).

-I: directly modify the contents of the read file, rather than output by the screen.

Action command:

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

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

D: delete, because it is a deletion, so there is usually nothing after d

G: global, indicating that the action command is executed globally within the line, that is, if there are multiple keywords in the line, delete or replace all of them

I: added, you can connect strings after I, and these strings will appear on the new line (the current previous line)

P: print, also print out a selected data, usually p will run with the parameter sed-n

S: replace, you can replace the work directly, usually the action of s can be matched with regular expressions.

New and deleted functions of ⑴

Sed can delete by line number in behavior units, such as listing the contents of the file to print the line number and deleting lines 2-5:

You can see that the standard output is missing 2-5 lines, and the last line can be referred to by "$". -e is omitted here, which is in command line mode by default, and can also be deleted based on keywords, such as deleting lines containing "CHEN":

However, the content of the file has not been changed at this time. For example, if you delete the contents of the original file and save it, you can use the-I parameter to execute the command directly on the file:

Note that although the first three lines are deleted at this time, the line number is still the beginning of the first line, because here nl is dealing with files rather than standard output. Next, let's add new line content. The example is as follows:

At this point, it is obvious that the difference between the two new commands an and I. You can use "\ + enter" to add multiple lines, as shown in the following example:

⑵ replacement and display function

The action command p in sed can display the content according to the line number, for example, select lines 5-7 in the display file:

The action command c in sed can replace the entire line, for example, replace lines 2-4 of the file with "reduplicates":

The action command s in sed can find and replace keywords in behavior units, where the keywords you want to find can be carried out with regular expressions, such as replacing all ":" with ";" in the file:

Where g represents the global, that is, every line is replaced entirely. If g is not added, only the first keyword found is replaced:

Sed can also process files directly. When dealing with multi-library data, when you encounter duplicate barcode, you often need to replace barcode, as shown below:

In the above example, the first item means that the replacement result is saved as a new file, while the latter two items are modified directly from the original file. Where "^" means to replace only the keywords that appear at the beginning of the line. Thank you for reading this article carefully. I hope the article "what's the use of sed" shared by the editor will be helpful to everyone? at the same time, I also hope that you will support and pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

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

Internet Technology

Wechat

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

12
Report