Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3043988
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2018-08-07 11:22:29

一、定义

sed 流编辑器,每次从输入中读取一行,用提供的编辑器命令匹配数据、按命令中指定的方式修改流中的数据,然后将生成的数据输出到STDOUT,在流编辑器将所 有命令与一行数据进行匹配后,它会读取下一行数据并重复这个过程,在流编辑器处理完流中的所有数据后,它就会终止。

二、格式

sed options script file

三、options选项

编号 option 解释
1 -n 不要为每隔命令生成输出,等待print命令来输出
2 -e script 可以运行多个命令
3 -f file 在处理时,将file中指定的命令添加到运行的命令中
4 -i 将修改后的结果写到源文件

新建test3.txt

[root@localhost shell]# cat test3.txt  101,Zhang san,Boss 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

1. -n

若使用 -n 选项,则不会打印输出结果,只有加上 p 选项,才可以输出结果
示例1:
打印第一行

[root@localhost shell]# sed -n '1 p' test3.txt  101,Zhang san,Boss [root@localhost shell]#  
  • 1
  • 2
  • 3

示例2:
打印1-3行

[root@localhost shell]# sed -n '1,3 p' test3.txt  101,Zhang san,Boss 102,Li si ,Manager 103,Wang wu,Programmer [root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5

示例3:
打印含有字符串”san”的那一行

[root@localhost shell]# sed -n '/san/ p' test3.txt  101,Zhang san,Boss [root@localhost shell]#  
  • 1
  • 2
  • 3

示例4:
打印含有字符串”san” 到字符串“wu”的所有行

[root@localhost shell]# sed -n '/san/,/wu/ p' test3.txt  101,Zhang san,Boss 102,Li si ,Manager 103,Wang wu,Programmer [root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5

示例5:
每隔一行,打印一次

[root@localhost shell]# sed -n '1~2 p' test3.txt  101,Zhang san,Boss 103,Wang wu,Programmer 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5

2. -e

可以运行多个命令
示例1:
打印第1行和第2行

[root@localhost shell]# sed -n -e  '1 p' -e '2 p' test3.txt  101,Zhang san,Boss 102,Li si ,Manager [root@localhost shell]# 
  • 1
  • 2
  • 3
  • 4

示例2:
打印第1行和第2行

[root@localhost shell]# sed -n -e  '1 p ; 2 p' test3.txt  101,Zhang san,Boss 102,Li si ,Manager [root@localhost shell]# 注意:两个命令之间必须用分号分割
  • 1
  • 2
  • 3
  • 4
  • 5

3. -f

新建一个test4.txt

[root@localhost shell]# cat test4.txt  1 p 2 p
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4

打印第一行和第二行

[root@localhost shell]# sed -n -f test4.txt test3.txt  101,Zhang san,Boss 102,Li si ,Manager [root@localhost shell]# 
  • 1
  • 2
  • 3
  • 4

四、替换命令

其格式为:
sed ‘[D] s/A/B/[C]’

D :为可选命令,可以表示执行的地方,如 1 只操作第一行, 1-3 操作 1-3 行, /san/ 只操作含有字符串”san”的那一行, /san/,/wu/ 操作从字符串”san” 到“wu”的行
A :需要被替换的字符
B :替换后的字符
C :可选命令,一些标志

1)基本操作

1.把第一行中的 ‘s’ 替换为 “666”

[root@localhost shell]# sed '1 s/s/666/' test3.txt  101,Zhang 666an,Boss 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注意:这里只是替换了第一行中的第一个 ‘s’,若想替换第一行中的全部 ‘3’,则需要使用可选标志 g ,再下面会介绍,这里先展示一下

[root@localhost shell]# sed '1 s/s/666/g' test3.txt  101,Zhang 666an,Bo666666 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.把1-3 行中的 ‘s’ 替换为 “666”

[root@localhost shell]# sed '1,3 s/s/666/' test3.txt  101,Zhang 666an,Boss 102,Li 666i ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.把含有字符”Li”那一行,中的‘s’ 替换为 “666”

[root@localhost shell]# sed '/Li/ s/s/666/' test3.txt  101,Zhang san,Boss 102,Li 666i ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4.把每一行中的‘s’ 替换为 “666”

此时,不需要前面的可选命令[D]即可
[root@localhost shell]# sed ' s/s/666/' test3.txt  101,Zhang 666an,Boss 102,Li 666i ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Cu666tomer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2)可选标志

标号 可选标志 解释
1 数字 表明新文本要替换第几处模式匹配的地方
2 g 表明新文本将会替换所有已有文本出现的地方
3 p 表明要将原来行的内容打印出来
4 w file 将替换后的结果写到file中去
5 i 忽略大小写

1.数字

在上面基本操作中,我们使用命令 sed ‘1 s/s/666/’ test3.txt 表示将第一行中的第一个 ‘s’替换为”666”,可以和这个对比一下

[root@localhost shell]# sed '1 s/s/666/2' test3.txt 101,Zhang san,Bo666s //替换第二个’s’ 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
[root@localhost shell]# sed '1 s/s/666/3' test3.txt 101,Zhang san,Bos666 //替换第三个’s’ 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.g

[root@localhost shell]# sed '1 s/s/666/g' test3.txt 101,Zhang 666an,Bo666666 //将第一行中的全部’s’ 替换为 “666” 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
[root@localhost shell]# sed 's/s/666/g' test3.txt 101,Zhang 666an,Bo666666 //将此文本中的所有’s’ 替换为 “666” 102,Li 666i ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Cu666tomer 666ervice
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.p

和-n 一起使用,只会打印出修改过的行

[root@localhost shell]# sed -n 's/s/666/gp' test3.txt  101,Zhang 666an,Bo666666 102,Li 666i ,Manager 105,Sun qi,Cu666tomer 666ervice
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5

4.w file

w file 选项只是将修改过的行输出到file中去

[root@localhost shell]# sed -n 's/s/666/gpw out.txt' test3.txt  101,Zhang 666an,Bo666666 102,Li 666i ,Manager 105,Sun qi,Cu666tomer 666ervice
[root@localhost shell]# cat out.txt  101,Zhang 666an,Bo666666 102,Li 666i ,Manager 105,Sun qi,Cu666tomer 666ervice
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

五、其它命令

文本替换命令并不是sed中的唯一命令,还有一些其它的命令

编号 其它命令 解释
1 d 删除
2 i 在指定行前增加一个新行
3 a 在指定行后增加一个新行
4 c 修改某一行的内容
5 y 转换命令
6 r 从文件中读取数据

新建一个test3.txt

[root@localhost shell]# cat test3.txt  101,Zhang san,Boss 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

1. d

删除命令

示例1:
删除第一行

[root@localhost shell]# sed '1d' test3.txt  102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

示例2:
删除第一道第二行

[root@localhost shell]# sed '1,2d' test3.txt  103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5

示例3:
删除含有字符”wu”的那一行

[root@localhost shell]# sed '/wu/d' test3.txt  101,Zhang san,Boss 102,Li si ,Manager 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2. i

在指定行的前面添加一个新行

示例1:
在第2行前面添加一个新行

[root@localhost shell]# sed '2 i\666' test3.txt  101,Zhang san,Boss 666 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

示例2:
在字符”wu”前面插入一个新行

[root@localhost shell]# sed '/wu/ i\666' test3.txt  101,Zhang san,Boss 102,Li si ,Manager 666 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3. a

在指定行的后面添加一个新行

示例1:
在第2行后面添加一个新行

[root@localhost shell]# sed '2 a\666' test3.txt  101,Zhang san,Boss 102,Li si ,Manager 666 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

示例2:
在字符”wu”后面插入一个新行

ot@localhost shell]# sed '/wu/ a\666' test3.txt  101,Zhang san,Boss 102,Li si ,Manager 103,Wang wu,Programmer 666 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4.c

其作用是替换某一行中的数据

示例1:
将第二行替换为 “666”

[root@localhost shell]# sed '2c 666' test3.txt  101,Zhang san,Boss 666 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

示例2:
将含有字符串”wu”的那一行替换为 “666”

[root@localhost shell]# sed '/wu/c 666' test3.txt  101,Zhang san,Boss 102,Li si ,Manager 666 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

5.y

转换命令是唯一可以处理单个字符的sed编辑器命令,格式如下:
y/A/B/
其功能是,A中的第一个字符被替换为B 中的第一个字符,A中的第二个字符被替换为B 中的第二个字符,A中的第三个字符被替换为B 中的第三个字符,直到A中的字符和B中额字符被一一对应完毕,也就是是说A中的字符长度要和B中的字符长度一致,若不一致,则会报错。

示例1:
将1替换为 7 2替换为 8 3 替换为9

[root@localhost shell]# sed 'y/123/789/' test3.txt  707,Zhang san,Boss 708,Li si ,Manager 709,Wang wu,Programmer 704,Zhao liu,HR 705,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

示例2:
将第二行中的1替换为 7 2替换为 8 3 替换为9

[root@localhost shell]# sed '2 y/123/789/' test3.txt  101,Zhang san,Boss 708,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

6.r

读取数据

示例1:

[root@localhost shell]# cat test5.txt 999 888 777 [root@localhost shell]# sed '3r test5.txt' test3.txt 101,Zhang san,Boss //在第三行后读入test5.txt中的数据 102,Li si ,Manager 103,Wang wu,Programmer 999 888 777 104,Zhao liu,HR 105,Sun qi,Customer service


[root@localhost shell]# sed '1,3r test5.txt' test3.txt 101,Zhang san,Boss //在第一道第三行,读取test5.txt中的数据 999 888 777 102,Li si ,Manager 999 888 777 103,Wang wu,Programmer 999 888 777 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

六、其它用法

1).替换字符

下面例子

#sed ‘s/\/bin\/bash/\/bin\/csh/’ /etc/passwd
  • 1

其目的是用C shell 替换/etc/passwd文件中的bash shell ,但是采用上面的方式看着比较困惑,因此可以采用替换字符的方式,如下:

seds!!!file #seds!/bin/bash!/bin/csh!’ /etc/passwd 
  • 1
  • 2
sed ‘@@@’ file #sed ‘s@/bin/bash@/bin/csh@’ /etc/passwd

阅读(1623) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~