[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 '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!