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 '/.*inet/p' 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 '/.*inet/p' | sed 's/.*inet\s\+//g' 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 '/.*inet/p' | sed 's/.*inet\s\+//g' | sed 's/\s\+.*//g' 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('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>
Let’s complete this requirement in several steps. First, filter all lines that do not contain URL addresses
# sed -n '/http:/p' a.txt
Then, replace all unnecessary information Blank characters are sufficient. First delete http:// and all the characters before it
sed -n '/http:/p' a.txt | sed 's/.*http:\/\///g'
, then delete all the double quotation marks and all the characters after it
# sed -n '/http:/p' 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!