龙空技术网

awk与sed序列(7)sed用法总结

最全钢琴谱 471

前言:

现在各位老铁们对“centos7蓝牙模块”大约比较注重,同学们都想要了解一些“centos7蓝牙模块”的相关资讯。那么小编在网上搜集了一些关于“centos7蓝牙模块””的相关文章,希望兄弟们能喜欢,你们一起来了解一下吧!

在Linux系统,sed简直就是一款不可多得的文本替换神器。

本文就总结了sed的一些基本用法。

首先把文本准备一下,如下所示

iv index.html

把下面文本复制进去

<HTML><BODY><H1>Welcome to my Homepage</H1> Content is divided into two sections:<BR><H2>ColdFusion</H2> Information about Macromedia ColdFusion.<H2>Wireless</H2> Information about Bluetooth, 802.11, and more.<H2>This is not valid HTML</H3> </BODY></HTML>
1. 查找替换

寻找有<H2>或者有<H1>的行,然后进行替换,即把W替换成AAAAAA,例子如下:

[root@centos ~]# sed '/<H1>/,/<H2>/ s/W/AAAAAA/g' index.html<HTML><BODY><H1>AAAAAAelcome to my Homepage</H1> Content is divided into two sections:<BR><H2>ColdFusion</H2> Information about Macromedia ColdFusion.<H2>Wireless</H2> Information about Bluetooth, 802.11, and more.<H2>This is not valid HTML</H3> </BODY></HTML>
2. 仅打印某个行

这个没啥好说的,直接看例子即可理解。

仅仅打印第10、11两行

[root@centos ~]# sed -n '10,11p' index.html </BODY></HTML>[root@centos ~]# cat -n index.html  1 <HTML> 2 <BODY> 3 <H1>Welcome to my Homepage</H1>  4 Content is divided into two sections:<BR> 5 <H2>ColdFusion</H2>  6 Information about Macromedia ColdFusion. 7 <H2>Wireless</H2>  8 Information about Bluetooth, 802.11, and more. 9 <H2>This is not valid HTML</H3>  10 </BODY> 11 </HTML>[root@centos ~]# 
3. 将命令应用于不匹配的特定模式的每一行

例子如下:

[root@centos ~]# sed '/H1/! s/W/AAAA/g' index.html <HTML><BODY><H1>Welcome to my Homepage</H1> Content is divided into two sections:<BR><H2>ColdFusion</H2> Information about Macromedia ColdFusion.<H2>AAAAireless</H2> Information about Bluetooth, 802.11, and more.<H2>This is not valid HTML</H3> </BODY></HTML>[root@centos ~]# 
4、替换特定字符串

替换第三个foo

[root@centos ~]# echo 'foot++++++foot++foosss' |sed 's/foo/bar/3'foot++++++foot++barsss

注意:为啥以下的sed是那样的

$ echo abc | sed 's/b*/1/'1abc$ echo abc | sed 's/b*/1/g'1a1c1

仔细观察字符开头和结尾有隐形的 ^ 和 $。

^表示一行的开头

$表示一行的结尾

^ 和 $ 符号无法匹配内嵌的换行字符,他们只用来表示字符串的开头和结尾。

5. sed 's/[0-9][0-9]$/&.5/' datafile

在替代串里的&字符代表在搜索串中真正找到的。

每个以两个数字结尾的行都被它自己取代,且要在后面加上.5

6. sed -n 's/Hemenway/Jones/gp' datafile

解释:所有的Hemenway所在的位置都用Jones来取代,而且只有改变的行被打印。

-n与p命令选项相结合来禁止默认输出。g代表全局替换

7. sed -n 's/\(Mar\)got/\1ianne/p' datafile

解释:模式Mar被封装在括弧里且在一个专用寄存器里存为标记1。在替换串里它将被引用做\1。然后用Marianne替代Margot。

8. sed 's#3#88#g' datafile

s命令后面的字符是搜索串和替换串之间的分界符。

默认的分界符是一个正斜杠,但也可以改变(只有使用s命令时)。

无论s命令后面跟什么字符,它都是新的串分界符。

当搜索包含一个正斜杠的模式,如路径或生日时,这种技巧可能有用

9. 从文件中读取:r命令

sed '/Suan/r newfile' datafile

解释:r命令从newfile中读取内容,将内容输出到Suan的后面。

如果datafile中Suan出现的次数不只一次,则分别放到Suan的后面。

[root@centos ~]# sed '/Suan/r newfile.txt' datafile.txt northwest NW Charles Main 3.0 .98 3 34western WE Sharon Gray 5.3 .97 5 23southwest SW Lewis Dalsass 2.7 .8 2 18southern SO Suan Chin 5.1 .95 4 1511111111111222222222southeast SE Patricia Hemenway 4.0 .7 4 17eastern EA TB Savage 4.4 .84 5 20northeast NE AM Main Jr. 5.1 .94 3 13north NO Margot Weber 4.5 .89 5 9central CT Ann Stephens 5.7 .94 5 13Suan11111111111222222222-----==+++[root@centos ~]# cat newfile.txt11111111111222222222[root@centos ~]# 
10. 写入文件:w命令
sed -n '/north/w newfile' datafile

解释:w命令把指定的行写入到一个文件。

本例中所有的包含north的行写入到newfile中。

等同于sed -n '/north/p' datafile >newfile

[root@centos ~]# sed '/north/w newfile.txt' datafile.txt northwest NW Charles Main 3.0 .98 3 34western WE Sharon Gray 5.3 .97 5 23southwest SW Lewis Dalsass 2.7 .8 2 18southern SO Suan Chin 5.1 .95 4 15southeast SE Patricia Hemenway 4.0 .7 4 17eastern EA TB Savage 4.4 .84 5 20northeast NE AM Main Jr. 5.1 .94 3 13north NO Margot Weber 4.5 .89 5 9central CT Ann Stephens 5.7 .94 5 13[root@centos ~]# cat newfile.txtnorthwest NW Charles Main 3.0 .98 3 34northeast NE AM Main Jr. 5.1 .94 3 13north NO Margot Weber 4.5 .89 5 9
11. 变换:y 命令
sed '1,3y/abcdefghijklmnopqrst/ABCDEFGHIJKLMNOPQRST/' datafile

解释将对应字母进行转换。

[root@centos ~]# sed '1,3y/abcdefghijklmnopqrst/ABCDEFGHIJKLMNOPQRST/' datafile.txt NORTHwEST NW CHARLES MAIN 3.0 .98 3 34wESTERN WE SHARON GRAy 5.3 .97 5 23SOuTHwEST SW LEwIS DALSASS 2.7 .8 2 18southern SO Suan Chin 5.1 .95 4 15southeast SE Patricia Hemenway 4.0 .7 4 17eastern EA TB Savage 4.4 .84 5 20northeast NE AM Main Jr. 5.1 .94 3 13north NO Margot Weber 4.5 .89 5 9central CT Ann Stephens 5.7 .94 5 13
12. 保存和取得:h和G命令
$ sed -e '/southeast/h' -e '$G' datafilenorthwest NW Charles Main 3.0 .98 3 34western WE Sharon Gray 5.3 .97 5 23southwest SW Lewis Dalsass 2.7 .8 2 18southern SO Suan Chin 5.1 .95 4 15southeast SE Patricia Hemenway 4.0 .7 4 17eastern EA TB Savage 4.4 .84 5 20northeast NE AM Main Jr. 5.1 .94 3 13north NO Margot Weber 4.5 .89 5 9central CT Ann Stephens 5.7 .94 5 13southeast SE Patricia Hemenway 4.0 .7 4 17

解释:当sed 处理文件时,每行都存在模式空间(pattern space)的临时缓存中。除非行被禁止打印或删除,否则行将在处理完后被打印到屏幕,然后请模式空间并把下一输入行保存在那里等待处理。在这个例子中,在找到模式之后,把它放在模式空间里,而且h命令复制它并把它存到另一个叫做保存缓存(holding buffer)中。第二个sed指令里,当读入最后一行($)时,G命令告诉sed从包存缓存中取得该行并放回模式空间缓存,添加到当前存在那里的行中。本例子就是最后一行。

$ sed -e '/WE/{h;d;}' -e '/CT/G' datafilenorthwest NW Charles Main 3.0 .98 3 34southwest SW Lewis Dalsass 2.7 .8 2 18southern SO Suan Chin 5.1 .95 4 15southeast SE Patricia Hemenway 4.0 .7 4 17eastern EA TB Savage 4.4 .84 5 20northeast NE AM Main Jr. 5.1 .94 3 13north NO Margot Weber 4.5 .89 5 9central CT Ann Stephens 5.7 .94 5 13western WE Sharon Gray 5.3 .97 5 23

解释:第一个命令h将找到了WE的行放到保存缓存中,然后删除该行;第二个命令/CT/G就是在找到了CT的行的后面加入保存缓存的内容。

13. G和g的区别

G命令在符合的条件行后面添加保存缓存中的内容;g命令用保存缓存中的内容覆盖符合条件的行。

标签: #centos7蓝牙模块