热门!!shell三剑客之“sed”

时间:2020-8-27 作者:admin


sed工具概述

sed(Stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除、替换、添加、移动等),最后输出所有行或者仅输出处理的某些行。sed 也可以在无交互的情况下实现相当复杂的文本处理操作,被广泛应用于 Shell 脚本中,用以完成各种自动化处理任务。
sed 的工作流程主要包括读取、执行和显示三个过程。

  • 读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)。
  • 执行:默认情况下,所有的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则 sed 命令将会在所有的行上依次执行。
  • 显示:发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。
    在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。

sed常见选项

选项 含义
-e 或–expression= 表示用指定命令或者脚本来处理输入的文本文件
-f 或–file= 表示用指定的脚本文件来处理输入的文本文件
-h 或–help 显示帮助
-n、–quiet 或 silent 表示仅显示处理后的结果
-i 直接编辑文本文件

常用操作

操作 含义
a 增加,在当前行下面增加一行指定内容
c 替换,将选定行替换为指定内容
d 删除,删除选定的行
i 插入,在选定行上面插入一行指定内容
p 打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用。
s 替换,替换指定字符
y 字符转换

sed用法示例

(1)输出符合条件的文本(p 表示正常输出)

[root@localhost ~]# sed -n 'p' test.txt  //输出所有内容,等同于 cat test.txt
123 123 123 123 123
xxx xxx xxx hhh hhh
the boy is not boy
this dog like cat
kdskldsf jkldjfsl

[root@localhost ~]#  sed -n '3p' test.txt   //输出第 3 行
the boy is not boy
[root@localhost ~]#  sed -n 'p;n' test.txt //输出奇数行
123 123 123 123 123
the boy is not boy
kdskldsf jkldjfsl
[root@localhost ~]# sed -n 'n;p' test.txt  //输出偶数行
xxx xxx xxx hhh hhh
this dog like cat
[root@localhost ~]# cat test.txt  //重新编辑测试文件,方便下面示例演示
1、123 123 123 123 123
2、xxx xxx xxx hhh hhh
3、the boy is not boy
4、this dog like cat
5、kdskldsf jkldjfsl
6、he was short and fat.
7、The home of Football on BBC Sport online. google is the best tools for search keyword.
8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
9、The year ahead will test our political establishment to the limit.
10、AxyzxyzxyzxyzC
11、Misfortunes never come alone/single.
12、PI=3.141592653589793238462643383249901429
13、a wood cross!
[root@localhost ~]# sed -n '10,${n;p}' test.txt //输出第10行至行尾的偶数行
11、Misfortunes never come alone/single.
13、a wood cross!
[root@localhost ~]# sed -n '/the/p' test.txt  //输出包含“the”的行
3、the boy is not boy
7、The home of Football on BBC Sport online. google is the best tools for search keyword.
8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
9、The year ahead will test our political establishment to the limit.
[root@localhost ~]# sed -n '4,/the/p' test.txt  //输出第4行至第一次出现包含“the”的行,若第4行以后没有the,将会输出第4 行以后的所有内容
4、this dog like cat
5、kdskldsf jkldjfsl
6、he was short and fat.
7、The home of Football on BBC Sport online. google is the best tools for search keyword.
[root@localhost ~]# sed -n '/the/=' test.txt //输出包含“the”的行的行号
3
7
8
9
[root@localhost ~]# sed -n '/^3/p' test.txt //输出以3开头的行
3、the boy is not boy
[root@localhost ~]#  sed -n '/\<wood\>/p' test.txt  //输出包含单词Word的行
13、a wood cross!
[root@localhost ~]# sed -n '/[0-9]$/p' test.txt  //输出以数字结尾的行
1、123 123 123 123 123
12、PI=3.141592653589793238462643383249901429

(2)删除符合条件的文本(d)

[root@localhost ~]# sed '3d' test.txt //删除第3行
1、123 123 123 123 123
2、xxx xxx xxx hhh hhh
4、this dog like cat
5、kdskldsf jkldjfsl
6、he was short and fat.
7、The home of Football on BBC Sport online. google is the best tools for search keyword.
8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
9、The year ahead will test our political establishment to the limit.
10、AxyzxyzxyzxyzC
11、Misfortunes never come alone/single.
12、PI=3.141592653589793238462643383249901429
13、a wood cross!

[root@localhost ~]# sed '/[0-9]$/d' test.txt //删除数字结尾的行
2、xxx xxx xxx hhh hhh
3、the boy is not boy
4、this dog like cat
5、kdskldsf jkldjfsl
6、he was short and fat.
7、The home of Football on BBC Sport online. google is the best tools for search keyword.
8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
9、The year ahead will test our political establishment to the limit.
10、AxyzxyzxyzxyzC
11、Misfortunes never come alone/single.
13、a wood cross!
[root@localhost ~]# sed '/\.$/d' test.txt //删除“.”结尾的行
1、123 123 123 123 123
2、xxx xxx xxx hhh hhh
3、the boy is not boy
4、this dog like cat
5、kdskldsf jkldjfsl
8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
10、AxyzxyzxyzxyzC
12、PI=3.141592653589793238462643383249901429
13、a wood cross!

(3)替换符合条件的文本

[root@localhost ~]# sed 's/the/THE/' test.txt  //将每行中的第一个the 替换为 THE
1、123 123 123 123 123
2、xxx xxx xxx hhh hhh
3、THE boy is not boy
4、this dog like cat
5、kdskldsf jkldjfsl
6、he was short and fat.
7、The home of Football on BBC Sport online. google is THE best tools for search keyword.
8、He was wearing a blue polo shirt with black pants. THE tongue is boneless but it breaks bones.12!
9、The year ahead will test our political establishment to THE limit.
10、AxyzxyzxyzxyzC
11、Misfortunes never come alone/single.
12、PI=3.141592653589793238462643383249901429
13、a wood cross!

[root@localhost ~]# sed 's/l/L/2' test.txt
1、123 123 123 123 123
2、xxx xxx xxx hhh hhh
3、the boy is not boy
4、this dog like cat
5、kdskldsf jkLdjfsl
6、he was short and fat.
7、The home of FootbalL on BBC Sport online. google is the best tools for search keyword.
8、He was wearing a blue poLo shirt with black pants. the tongue is boneless but it breaks bones.12!
9、The year ahead wilL test our political establishment to the limit.
10、AxyzxyzxyzxyzC
11、Misfortunes never come alone/singLe.
12、PI=3.141592653589793238462643383249901429
13、a wood cross!

[root@localhost ~]# sed 's/^/#/' test.txt  //在每行行首插入#号
#1、123 123 123 123 123
#2、xxx xxx xxx hhh hhh
#3、the boy is not boy
#4、this dog like cat
#5、kdskldsf jkldjfsl
#6、he was short and fat.
#7、The home of Football on BBC Sport online. google is the best tools for search keyword.
#8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
#9、The year ahead will test our political establishment to the limit.
#10、AxyzxyzxyzxyzC
#11、Misfortunes never come alone/single.
#12、PI=3.141592653589793238462643383249901429
#13、a wood cross!
[root@localhost ~]# sed '/the/s/^/#/' test.txt
1、123 123 123 123 123
2、xxx xxx xxx hhh hhh
#3、the boy is not boy
4、this dog like cat
5、kdskldsf jkldjfsl
6、he was short and fat.
#7、The home of Football on BBC Sport online. google is the best tools for search keyword.
#8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
#9、The year ahead will test our political establishment to the limit.
10、AxyzxyzxyzxyzC
11、Misfortunes never come alone/single.
12、PI=3.141592653589793238462643383249901429
13、a wood cross!

[root@localhost ~]# sed -n '3,5s/o/O/gp' test.txt  //将3到5行的o换成O
3、the bOy is nOt bOy
4、this dOg like cat

(4)迁移符合条件的文本

在使用 sed 命令迁移符合条件的文本时,常用到以下参数.

参数 含义
H 复制到剪贴板
g、G 将剪贴板中的数据覆盖/追加至指定行
w 保存为文件
r 读取指定文件
a 追加指定内容
[root@localhost ~]# sed '/the/{H;d};$G' test.txt  //将包含the 的行迁移至文件末尾,{;}用于多个操作
1、123 123 123 123 123
2、xxx xxx xxx hhh hhh
4、this dog like cat
5、kdskldsf jkldjfsl
6、he was short and fat.
10、AxyzxyzxyzxyzC
11、Misfortunes never come alone/single.
12、PI=3.141592653589793238462643383249901429
13、a wood cross!

3、the boy is not boy
7、The home of Football on BBC Sport online. google is the best tools for search keyword.
8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
9、The year ahead will test our political establishment to the limit.

[root@localhost ~]# sed '/the/r /etc/hostname' test.txt  //将文件/etc/hostname 的内容添加到包含 the 的每行以后
1、123 123 123 123 123
2、xxx xxx xxx hhh hhh
3、the boy is not boy
localhost.localdomain
4、this dog like cat
5、kdskldsf jkldjfsl
6、he was short and fat.
7、The home of Football on BBC Sport online. google is the best tools for search keyword.
localhost.localdomain
8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
localhost.localdomain
9、The year ahead will test our political establishment to the limit.
localhost.localdomain
10、AxyzxyzxyzxyzC
11、Misfortunes never come alone/single.
12、PI=3.141592653589793238462643383249901429
13、a wood cross!

[root@localhost ~]# sed '3aNew' test.txt   //在第 3 行后插入一个新行,内容为New
1、123 123 123 123 123
2、xxx xxx xxx hhh hhh
3、the boy is not boy
New
4、this dog like cat
5、kdskldsf jkldjfsl
6、he was short and fat.
7、The home of Football on BBC Sport online. google is the best tools for search keyword.
8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
9、The year ahead will test our political establishment to the limit.
10、AxyzxyzxyzxyzC
11、Misfortunes never come alone/single.
12、PI=3.141592653589793238462643383249901429
13、a wood cross!

[root@localhost ~]# sed '3aNew1\nNew2' test.txt   //在第 3 行后插入多行内容,中间的\n 表示换行
1、123 123 123 123 123
2、xxx xxx xxx hhh hhh
3、the boy is not boy
New1
New2
4、this dog like cat
5、kdskldsf jkldjfsl
6、he was short and fat.
7、The home of Football on BBC Sport online. google is the best tools for search keyword.
8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
9、The year ahead will test our political establishment to the limit.
10、AxyzxyzxyzxyzC
11、Misfortunes never come alone/single.
12、PI=3.141592653589793238462643383249901429
13、a wood cross!

(5)使用脚本编辑文件

[root@localhost ~]# sed '1,5{H;d};17G' test.txt  //将第 1~5 行内容转移至第 17 行后
6、he was short and fat.
7、The home of Football on BBC Sport online. google is the best tools for search keyword.
8、He was wearing a blue polo shirt with black pants. the tongue is boneless but it breaks bones.12!
9、The year ahead will test our political establishment to the limit.
10、AxyzxyzxyzxyzC
11、Misfortunes never come alone/single.
12、PI=3.141592653589793238462643383249901429
13、a wood cross!

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。