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

Skills of using sed Command in Linux production Environment

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

Share

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

This article will explain in detail the skills of using sed commands in Linux production environment. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Sed command is widely used and easy to use, and it is a sharp tool for fast text processing. In fact, it does not have many skills, recitation and use is the most appropriate learning channel, which belongs to hard skills. But it is also complicated because there are too many advanced features. This article does not pay attention to the advanced functions of sed, but only explains some commonly used operations.

As you use it, you will find that it works well with some of the ideas of vim, and the syntax of regular expressions is basically the same, with little learning cost. From the perspective of personal vision and productivity, sed command is an important tool that programmers must master.

Those who say that they can use google on the spot, most of them are used to copying the text into excel, slowly idling their work, and are in a hurry when they encounter a large number of files. It is not that the whole family does not enter the same door. This article is not written for you.

A simple introduction

As shown in the figure, a simple sed command consists of three main parts: parameters, scope, and operation. The file you want to manipulate can be hung directly at the end of the command line. In addition to the command line, sed can also specify a sed script with the-f argument, which is an advanced usage without too much description.

Some example commands I will repeat many times, smart as you must be able to find the rules, and sometimes do not even need to explain.

Parameters.

The parameter-n means-- quiet or-- silent. Indicates that the output of the execution process is ignored and only our results are output.

There is another parameter that we commonly use:-I.

After using this parameter, all changes will be performed on the original file. Your output will overwrite the original file. It's very dangerous. We must pay attention to it.

Range

1Jing 4 means to find the contents of the 4lines of 1pm 2pm 3pm in the file.

The designation of this range is very intelligent, please see the following example (please replace the scope part of the figure yourself).

Regular matching can also be used for range selection. See the following example.

For the sake of intuition, the following commands correspond to the above introduction one by one, and there can be spaces between the scope and the operation.

Operation

The most common operation is p, which means printing. For example, the following two commands are equivalent:

In addition to printing, there are the following operations that we often use.

Although the operations such as arecom iPol c are basic, they are seldom used and are not introduced. We still come up with some orders to illustrate.

Let's take a look at what sed commands can do and try it with some commands.

Delete all lines starting with # and blank lines.

The most commonly used ones, such as the one below.

Represents the second line in the printed group file.

So what if I want to execute more than one command at a time and don't want to write a sed script file? Then you need to add the-e parameter.

The operation unit of sed is the line.

Replacement mode

The above is a common matching pattern for the sed command, but it also has a powerful replacement pattern, which means to find and replace some of the values and output the results. The-n parameter is rarely used in replacement mode.

The parameters of the replacement mode are a little too many, but the first and fifth parts can be omitted. The entire text will be output after replacement.

The first half is used to match some ranges, while the second half performs the action of replacement.

Range

This scope is similar to the scope syntax above. Look at the following example.

The specific commands are:

Command

The command here refers to s. That's what substitute means.

Find a match

The search section will find the string to be replaced. This part can accept either pure strings or regular expressions. Look at the following example.

The command is similar to:

Replace

It's time to replace the string found. The content of this section replaces what is found in the find matching section.

Unfortunately, regularities cannot be used in this part. What is commonly used is precise replacement. For example, replace a with b.

But it also has advanced features. Similar to the regular api of java or python, the replacement of sed also has the meaning of Matched Pattern, and you can also get Group. The commonly used substitute is. Number, repeat it again. When it is used in a replacement string, it represents the original search matching data.

The following command will enclose every line in the file in quotation marks.

Flag parameter

These parameters can be used individually or multiple, and only the most commonly used ones are described.

Look at the syntax of the two commands:

Because of the regularity, many characters need to be escaped. You will do a lot of things like\\,\ * in the script. You can use | ^ @! Four characters to replace\.

For example, the following five commands are the same.

Note: the scope of the first half cannot be used in this way. I am used to using the symbol @.

Other

Regular expression

As you can see, regular expressions are everywhere on the command line. The following is a brief explanation.

Parameter I

The parameter I has been briefly introduced above, and its function is to let the operation be performed in the original file. No matter what you execute, the original file will be overwritten. This is very dangerous.

By adding a parameter, the original file can be backed up.

Sed-i.bak 's Universe a Universe b Universe 'file

The above command takes effect on the original file file and generates a file.bak file. It is strongly recommended to use the I parameter to specify the bak file at the same time.

Let's take a look at the combined power of sed and other commands with two commands.

Output lines with a length of not less than 50 characters

Count how many times each word appears in the file

Find the py file in the directory and delete all line-level comments

View lines 5-7 and 10-13

Output only ip addresses

So much for sharing the skills of using sed commands in Linux production environment. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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