Home  >  Article  >  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

齐天大圣
齐天大圣Original
2020-09-17 17:27:451769browse

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

# 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

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

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

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

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

Example 3: Get the server ip address

This case is very good, I recommend it to everyone Take a closer look

# 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)
……

What we need is 172.26.9.143.

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

# ifconfig eth0 | sed -n &#39;/.*inet/p&#39;
        inet 172.26.9.143  netmask 255.255.240.0  broadcast 172.26.15.255

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

# ifconfig eth0 | sed -n &#39;/.*inet/p&#39; | sed &#39;s/.*inet\s\+//g&#39;
172.26.9.143  netmask 255.255.240.0  broadcast 172.26.15.255

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

# ifconfig eth0 | sed -n &#39;/.*inet/p&#39; | sed &#39;s/.*inet\s\+//g&#39; | sed &#39;s/\s\+.*//g&#39;
172.26.9.143

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:

  <td width=820> <a href=http://beijing.hellozx.com onclick="co(&#39;beijing&#39;)"><font color="red">北京</font></a>
     <a href=http://shanghai.hellozx.com onclick="co(&#39;shanghai&#39;)"><font color="red">上海</font></a>
     <a href=http://tianjin.hellozx.com onclick="co(&#39;tianjin&#39;)"><font color="red">天津</font></a>
     <a href=http://chongqing.hellozx.com onclick="co(&#39;chongqing&#39;)"><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(&#39;jinan&#39;)"><font color=red style="font-size:14px;">济南</font></a>
 <a href="http://qingdao.hellozx.com" onclick="co(&#39;qingdao&#39;)"><font color=red style="font-size:14px;">青岛</font></a>

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

# sed -n &#39;/http:/p&#39; a.txt

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

sed -n &#39;/http:/p&#39; a.txt | sed &#39;s/.*http:\/\///g&#39;

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

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

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:
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