


Understand the sed command of one of the three swordsmen of shell programming in one article
Sep 17, 2020 pm 05:27 PMAs one of the three swordsmen of shell programming, the importance of sed is self-evident. sed is a stream programmer that processes one line at a time, and then processes the next line after processing. It supports regular expressions and is very powerful. However, sed is more complicated than ordinary commands and has many options. It takes some effort to master it. Let's take a look at the role of this powerful sed command through many examples!
Syntax format: sed [option] [action]
Commonly used options are as follows:
-n: sed will be used by default Output all stdin contents, but with this option, only lines processed by sed will be displayed.
-e: Allow multiple actions to be executed on the same line
-f: Read actions from a file
-i: Modify the file directly instead of inputting it on the screen (danger)
There are too many parameters in the action, list them below:
a: Add a line below the matching line
c: Modify the matching line with new content
d: Delete matching lines
i: Add content before matching lines
p: Print
s: Replace the contents of matching lines
Example 1: Print lines 5-10 of the /etc/passwd file
1 2 3 4 5 6 7 |
|
Example 2: Delete line 2 of the /etc/passwd file, lines 2 to 10
1 |
|
In addition to the above method , we can also use the -e option to complete
1 |
|
Example 3: Get the server ip address
This case is very good, I recommend it to everyone Take a closer look
1 2 3 4 5 |
|
What we need is 172.26.9.143.
The first step is to get the line we want to process
1 2 |
|
The second step is to replace the unnecessary information with blank characters
1 2 |
|
Next, put the ip address Just leave all subsequent replacement bits empty
1 2 |
|
Let’s talk about the above example. First, in the first step, we use -n and p to get the lines we want. Then, we filter the unnecessary information. It should be noted that \s in the regular expression means matching at least one space, but here you need to use the escape character \ before the plus sign.
Example 4
For the last example, I give a real case I had yesterday. The requirement is like this, get the url address in a piece of text, but the previous http:// is not needed. Part of the text content is as follows:
1 2 3 4 5 6 7 8 |
|
Let’s complete this requirement in several steps. First, filter all lines that do not contain URL addresses
1 |
|
Then, replace all unnecessary information Blank characters are sufficient. First delete http:// and all the characters before it
1 |
|
, then delete all the double quotation marks and all the characters after it
1 |
|
Now the screen output is as follows
haikou.hellozx .com sanya.hellozx.com kunming.hellozx.com dali.hellozx.com xining.hellozx.com yinchuan.hellozx.com wulumuqi.hellozx.com ……
The above is the detailed content of Understand the sed command of one of the three swordsmen of shell programming in one article. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to install Classic Shell on Windows 11?
![Explorer.exe does not start on system startup [Fix]](https://img.php.cn/upload/article/000/887/227/168575230155539.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Explorer.exe does not start on system startup [Fix]

PowerShell deployment fails with HRESULT 0x80073D02 issue fixed

How to quickly delete the line at the end of a file in Linux

Different ways to run shell script files on Windows

Super hardcore! 11 very practical Python and Shell script examples!

Here are the fixes for Open Shell Windows 11 not working issue

109 practical shell script examples, the code is clear and easy to use!
