Home Operation and Maintenance Linux Operation and Maintenance Understand the sed command of one of the three swordsmen of shell programming in one article

Understand the sed command of one of the three swordsmen of shell programming in one article

Sep 17, 2020 pm 05:27 PM
shell

As 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

# sed -n '5,10p' /etc/passwd

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

Copy after login

Example 2: Delete line 2 of the /etc/passwd file, lines 2 to 10

1

# cat -n /etc/passwd | sed '2d' | sed '5,10d'

Copy after login

In addition to the above method , we can also use the -e option to complete

1

# cat -n /etc/passwd | sed -e '2d' -e '5,10d'

Copy after login

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

# ifconfig eth0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 172.26.9.143  netmask 255.255.240.0  broadcast 172.26.15.255

        ether 00:16:3e:0a:01:ad  txqueuelen 1000  (Ethernet)

……

Copy after login

What we need is 172.26.9.143.

The first step is to get the line we want to process

1

2

# ifconfig eth0 | sed -n '/.*inet/p'

        inet 172.26.9.143  netmask 255.255.240.0  broadcast 172.26.15.255

Copy after login

The second step is to replace the unnecessary information with blank characters

1

2

# ifconfig eth0 | sed -n '/.*inet/p' | sed 's/.*inet\s\+//g'

172.26.9.143  netmask 255.255.240.0  broadcast 172.26.15.255

Copy after login

Next, put the ip address Just leave all subsequent replacement bits empty

1

2

# ifconfig eth0 | sed -n '/.*inet/p' | sed 's/.*inet\s\+//g' | sed 's/\s\+.*//g'

172.26.9.143

Copy after login

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

  <td width=820> <a href=http://beijing.hellozx.com onclick="co('beijing')"><font color="red">北京</font></a>

     <a href=http://shanghai.hellozx.com onclick="co('shanghai')"><font color="red">上海</font></a>

     <a href=http://tianjin.hellozx.com onclick="co('tianjin')"><font color="red">天津</font></a>

     <a href=http://chongqing.hellozx.com onclick="co('chongqing')"><font color="red">重庆</font></a></td></tr>

<tr><td width=80 height=30 align=right>

<b><font style="font-size:14px;">山东</font>:</b> </td><td width=820>

 <a href="http://jinan.hellozx.com" onclick="co('jinan')"><font color=red style="font-size:14px;">济南</font></a>

 <a href="http://qingdao.hellozx.com" onclick="co('qingdao')"><font color=red style="font-size:14px;">青岛</font></a>

Copy after login

Let’s complete this requirement in several steps. First, filter all lines that do not contain URL addresses

1

# sed -n '/http:/p' a.txt

Copy after login

Then, replace all unnecessary information Blank characters are sufficient. First delete http:// and all the characters before it

1

sed -n '/http:/p' a.txt | sed 's/.*http:\/\///g'

Copy after login

, then delete all the double quotation marks and all the characters after it

1

# sed -n '/http:/p' a.txt | sed 's/.*http:\/\///g' | sed 's/".*//g'

Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install Classic Shell on Windows 11? How to install Classic Shell on Windows 11? Apr 21, 2023 pm 09:13 PM

How to install Classic Shell on Windows 11?

Explorer.exe does not start on system startup [Fix] Explorer.exe does not start on system startup [Fix] Jun 03, 2023 am 08:31 AM

Explorer.exe does not start on system startup [Fix]

PowerShell deployment fails with HRESULT 0x80073D02 issue fixed PowerShell deployment fails with HRESULT 0x80073D02 issue fixed May 10, 2023 am 11:02 AM

PowerShell deployment fails with HRESULT 0x80073D02 issue fixed

How to quickly delete the line at the end of a file in Linux How to quickly delete the line at the end of a file in Linux Mar 01, 2024 pm 09:36 PM

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

Different ways to run shell script files on Windows Different ways to run shell script files on Windows Apr 13, 2023 am 11:58 AM

Different ways to run shell script files on Windows

Super hardcore! 11 very practical Python and Shell script examples! Super hardcore! 11 very practical Python and Shell script examples! Apr 12, 2023 pm 01:52 PM

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

Here are the fixes for Open Shell Windows 11 not working issue Here are the fixes for Open Shell Windows 11 not working issue Apr 14, 2023 pm 02:07 PM

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! 109 practical shell script examples, the code is clear and easy to use! Aug 02, 2023 pm 03:25 PM

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

See all articles