1.概述

sed是一个功能十分强大的文本处理命令,本文主要介绍一些常用的文本处理方法。

2.总述

2.1sed命令行格式

​ sed [-nefri] ‘command’ 输入文本/文件

2.2 常用选项

​ -n∶取消默认的输出,使用安静(silent)模式。

    -e∶进行多项编辑,即对输入行应用多条sed命令时使用. 直接在指令列模式上进行 sed 的动作编辑

    -f∶指定sed脚本的文件名. 直接将 sed 的动作写在一个档案内, -f filename 则可以执行 filename 内的sed 动作

    -r∶sed 的动作支援的是延伸型正则表达式的语法。(预设是基础正则表达式语法)

    -i∶直接修改读取的文件内容,而不是由屏幕输出       

2.3 常用命令

   p∶ 列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起用

​ i ∶ 插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行)

​ a ∶ 新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)

​ d ∶ 删除,因为是删除,所以 d 后面通常不接任何内容

​ c ∶ 取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行

​ s∶ 取代,可以直接进行替换的工作。通常这个 s 的动作可以搭配正则表达式。例如 1,20s/old/new/g

​ / /: 模式匹配

3.各选项用法

3.1 -i

直接修改文件,不带该选项是不会操作文件,只会显示处理结果

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
34
35
36
37
38
39
40
41
42
43

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart root: hello

Apr 21 15:34:59 smart root: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed "s/root/user/g" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart root: hello

Apr 21 15:34:59 smart root: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed -i "s/root/user/g" test.log

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

3.2 -n

只显示操作结果,而不显示中间过程,即不显示文本内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

[root@smart Desktop]# sed "/user/p" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed -n "/user/p" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

3.3 p

​ 查询结果打印输出,如打印1,2行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

[root@smart Desktop]# sed -n "1,2p" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

[root@smart Desktop]# sed -n "/user/,/15:45:55/p" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

3.4 a/i

添加内容

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#在第一行后添加hello world

[root@smart Desktop]# sed "1a hello world" test.log

Apr 21 15:34:49 smart user: hello

hello world

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

#在每一行添加hello world

[root@smart Desktop]# sed "a hello world" test.log

Apr 21 15:34:49 smart user: hello

hello world

Apr 21 15:34:59 smart user: hello2

hello world

Apr 22 15:45:55 smart test1.bin: test1.c main 15

hello world

Apr 22 15:46:08 smart test1.bin: test1.c main 15

hello world

#第一行前插入

[root@smart Desktop]# sed "1i hello world" test.log

hello world

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

#最后一行插入

[root@smart Desktop]# sed '$a bye' test.log

Apr 21 15:34:49 smart user: hello

$ *

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

bye

3.5 d

删除指定内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed '1d' test.log

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

删除包含user到第15:45:55行的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]#

[root@smart Desktop]# sed "/user/,/15:45:55/d" test.log

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]#

3.6 模式匹配查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed -n '/hello2/' test.log

sed: -e expression #1, char 8: missing command

[root@smart Desktop]# sed -n '/hello2/p' test.log

Apr 21 15:34:59 smart user: hello2



3.7 替换一行或多行 c

替换1,2行为hello

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

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed -n '1,2c hello' test.log

hello

[root@smart Desktop]# sed '1,2c hello' test.log

hello

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15



3.8 替换字符串

将smart替换为test,常加上g表示全局替换

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

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed "s/smart/test/" test.log

Apr 21 15:34:49 test user: hello

Apr 21 15:34:59 test user: hello2

Apr 22 15:45:55 test test1.bin: test1.c main 15

Apr 22 15:46:08 test test1.bin: test1.c main 15

[root@smart Desktop]# sed "s/smart/test/g" test.log

Apr 21 15:34:49 test user: hello

Apr 21 15:34:59 test user: hello2

Apr 22 15:45:55 test test1.bin: test1.c main 15

Apr 22 15:46:08 test test1.bin: test1.c main 15



3.9 显示文件行号 =

1
2
3
4
5
6
7
8
9
10
11

[root@smart Desktop]# sed -n "/smart/=" test.log

1

3

4

5

3.10 sed 结果写入到文件w

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#l表示显示特殊字符

[root@smart Desktop]# sed -n "/smart/l" test.log

Apr 21 15:34:49 smart user: hello$

Apr 21 15:34:59 smart user: hello2$

Apr 22 15:45:55 smart test1.bin: test1.c main 15 $

Apr 22 15:46:08 smart test1.bin: test1.c main 15 $

[root@smart Desktop]# sed -n "/user/ w test.log2" test.log

[root@smart Desktop]# cat test.log2

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

[root@smart Desktop]#

###3.11 替换路径

[root@smart Desktop]# pwd

/root/Desktop

[root@smart Desktop]# pwd|sed “s?${PWD}?${PWD}/test?g”

/root/Desktop/test

4 注意事项

对于特殊字符的操作需要注意

* 代表任意多个

^ 任意开头 ^[1-9]任意1-9开头的数字

$ 行结束符

\ 转义字符

. 任意字符

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed "s/^smart/abc/g" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed "s/^Apr/abc/g" test.log

abc 21 15:34:49 smart user: hello

abc 21 15:34:59 smart user: hello2

abc 22 15:45:55 smart test1.bin: test1.c main 15

abc 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed "s/user.*/abc/g" test.log

Apr 21 15:34:49 smart abc

Apr 21 15:34:59 smart abc

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed "s/^.*$/abc/g" test.log

abc

abc

abc

abc

[root@smart Desktop]# sed -i "1a \$ \* " test.log

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

$ *

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed -n "/\$/p" test.log

Apr 21 15:34:49 smart user: hello

$ *

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15

[root@smart Desktop]# sed -n "/\*/p" test.log

$ *

[root@smart Desktop]# sed -n "#\$#p" test.log

[root@smart Desktop]# sed -n "#$#p" test.log

[root@smart Desktop]# sed -n "s#test#abc#g" test.log

[root@smart Desktop]# sed "s#test#abc#g" test.log

Apr 21 15:34:49 smart user: hello

$ *

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart abc1.bin: abc1.c main 15

Apr 22 15:46:08 smart abc1.bin: abc1.c main 15

[root@smart Desktop]# sed "#$#p" test.log

Apr 21 15:34:49 smart user: hello

$ *

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15

Apr 22 15:46:08 smart test1.bin: test1.c main 15