替换元字符
& 用正则表达式匹配的内容进行替换
\n 换行
\ 转义符
1.将文件换行
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
[root@node1 ~]# sed 's/\t/\
>/2' abc
Column1 Column2
Column3 Column4
[root@node1 ~]# sed 's/\t/\n/2' abc
Column1 Column2
Column3 Column4
[root@node1 ~]# sed 's/\t/\n/3' abc
Column1 Column2 Column3
Column4
[root@node1 ~]#
2. .Ah "Major Heading"替换@A HEAD = Major Heading
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
[root@node1 ~]# sed '/^\.Ah/{
> s/\.Ah */\
> @A HEAD =/> s/"//g
> s/$/\
>/}' abc
Column1 Column2 Column3 Column4
@A HEAD = Major Heading
[root@node1 ~]#
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"1)
[root@node1 ~]# sed '/^\.Ah/p' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
.Ah "Major Heading"2)
[root@node1 ~]# sed '/^\.Ah/s/\.Ah */@A HEAD =/' abc
Column1 Column2 Column3 Column4
@A HEAD ="Major Heading"
[root@node1 ~]#
3)
[root@node1 ~]# sed '/^\.Ah/{s/\.Ah */@A HEAD =/;s/"//g}' abc Column1 Column2 Column3 Column4
@A HEAD = Major Heading
[root@node1 ~]#
4)
[root@node1 ~]# sed '/^\.Ah/{s/\.Ah */\n@A HEAD =/;s/"//g}' abc
Column1 Column2 Column3 Column4
@A HEAD = Major Heading
5)
[root@node1 ~]# sed '/^\.Ah/{s/\.Ah */\n@A HEAD =/;s/"//g};s/$/\n/' abc
Column1 Column2 Column3 Column4
@A HEAD = Major Heading
[root@node1 ~]#
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
2.5 例4
& 匹配整行内容
1.
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
[root@node1 ~]# sed "s/ORA/& Associates, Inc./g" abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc. Associates, Inc.
[root@node1 ~]#
ORA Associates, Inc.替换O' Reilly ORA Associates, Inc.
2.
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
1)
[root@node1 ~]# sed -r "s/ORA (.*)/O' Reilly &/g" abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
O' Reilly ORA Associates, Inc.
2)
[root@node1 ~]# sed -r "s/ORA/O' Reilly &/g" abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
O' Reilly ORA Associates, Inc.
[root@node1 ~]#
3.
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
1)在ORA后面加1234
s/ORA/只匹配ORA
[root@node1 ~]# sed 's/ORA/&1234/g' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA 1234 Associates, Inc.
2)在行尾加1234
s/ORA.*/ 匹配整行
[root@node1 ~]# sed 's/ORA.*/&1234/g' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc. 1234
[root@node1 ~]# sed 's/ORA.*/&1234/g' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.1234
[root@node1 ~]#
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
2.6 例5
\s \\s
UNIX换成\s-2UNIX\s0
1.
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
[root@node1 ~]# sed 's/UNIX/\\s-2&\\s0/g' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the \s-2UNIX\s0 Operating System.
[root@node1 ~]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2.7 例6
See Section 1.4
See Section 12.9
将上面两行()
1.
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
[root@node1 ~]# sed 's/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/g' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
(See Section 1.4)(See Section 12.9)2.
[root@node1 ~]# sed -r 's/See Section [1-9][0-9]*.[1-9][0-9]*/(&)/g' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
(See Section 1.4)(See Section 12.9)
[root@node1 ~]#
See Section\fb1.4\fp
See Section\fb12.9\fp3.
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
[root@node1 ~]# sed -r 's/(See Section)([1-9][0-9]*.[1-9][0-9]*)/\1\\fb\2\\fp/g' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section\fb1.4\fp
See Section\fb12.9\fp
[root@node1 ~]#
或
[root@node1 ~]# sed -r 's/(.*)([1-9][0-9]*.[1-9][0-9]*)/\1\\fb\2\\fp/g' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section\fb1.4\fp
See Section\fb12.9\fp
[root@node1 ~]#
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
2.8 例7
first:second
one:two
将上面两行调换位置
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@node1 ~]# sed -r 's/(.*):(.*)/\2:\1/g' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
second:first
two:one
[root@node1 ~]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2.9 例8
substitution换成substitute
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]# sed '/^\.XX /s/substitution/substitute/g' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
.XX "sed, substitute command"
[root@node1 ~]#
s/^\.XX \(.*\)$/\/^\\.XX \/s\/\1/\1\//
s#^\.XX (.*)$#/^\\.XX /s\1\1#
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
3. 删除 d
3.1 例1
[root@node1 ~]# cat -n abc
1 Column1 Column2 Column3 Column4
2 .Ah "Major Heading"3 aAh Major Heading
4 zAh Major Heading
5 ORA Associates, Inc.
6on the UNIX Operating System.
7 See Section 1.48 See Section 12.99 first:second
10 one:two
11 .XX "sed, substitution command"1.删除.Ah开头的
[root@node1 ~]# sed '/.Ah/d' abc
Column1 Column2 Column3 Column4
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]#
2.删除.Ah开头的
[root@node1 ~]# sed '/\.Ah/d' abc
Column1 Column2 Column3 Column4
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]#
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
.XX "sed, substitution command"1.在第一行后面加abc
[root@node1 ~]# sed '1a abc' abc
Column1 Column2 Column3 Column4
abc
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]#
2.在第一行后面加abc ddas
[root@node1 ~]# sed '1a abc ddas' abc
Column1 Column2 Column3 Column4
abc ddas
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]#
3.在第一行后面加 abc
[root@node1 ~]# sed '1a\ abc' abc
Column1 Column2 Column3 Column4
abc
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]#
4.利用匹配的方式在zAh Major Heading后加 abcdes
[root@node1 ~]# sed '/^zAh/a \ abcdes' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
abcdes
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]#
5.在See后加abcdes
[root@node1 ~]# sed '/^See/a abcdes' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
abcdes
See Section 12.9
abcdes
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]#
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"
See Section 12.9
See Section 13.56. 上面两行后加abc
[root@node1 ~]# sed '/See.*[1-9][0-9]\.[0-9]/a abc' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
abc
See Section 13.5
abc
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]#
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
4.2 例2 插入 i
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"1.在第一行插入mushuang
[root@node1 ~]# sed '1i mushuang' abc
mushuang
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]#
或
[root@node1 ~]# sed '/^\.Ah/i abc' abc
Column1 Column2 Column3 Column4
abc
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]#
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
4.3 例3 更改 c
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"1.将ORA Associates, Inc.改成papap
[root@node1 ~]# sed '/ORA/c papap' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
papap
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"
[root@node1 ~]#
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
4.4 例4 i
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"
Larry' s Address
1.Larry' s Address后加两行
[root@node1 ~]# sed '/Larry/i 4700 Cross Court\
> French Lick,IN> ' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"4700 Cross Court
French Lick,IN
Larry' s Address
[root@node1 ~]#
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
4.5 例5
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"
Larry' s Address
[root@node1 ~]#
1. .Ah开头,on结尾替换 abcd
[root@node1 ~]# sed '/^\.Ah/,/on/c abcd' abc
Column1 Column2 Column3 Column4
abcd
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"
Larry' s Address
[root@node1 ~]#
2.全部内容替换成abcde
[root@node1 ~]# sed '/^Column/,$c abcde' abc
abcde
[root@node1 ~]#
或者
[root@node1 ~]# sed '1,$c abcde' abc
abcde
[root@node1 ~]#
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
5. 转换 y
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
[root@node1 ~]# cat abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"
aAh Major Heading
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"
Larry' s Address
1.将第三行中的ajg转换成978
[root@node1 ~]# sed '3y/ajg/978/' abc
Column1 Column2 Column3 Column4
.Ah "Major Heading"9Ah M97or He9din8
zAh Major Heading
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
.XX "sed, substitution command"
Larry' s Address
[root@node1 ~]#
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
6. 打印 p
6.1 例
[root@node1 ~]# cat bca
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "List"1.
[root@node1 ~]# sed '/^\.Ah/{p}' bca
.Ah "Comment"
.Ah "Comment"
.Ah "Substitution"
.Ah "Substitution"
.Ah "Delete"
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "Append, Insert and Change"
.Ah "List"
.Ah "List"
[root@node1 ~]#
2.
[root@node1 ~]# sed '/^\.Ah/{p;s/"//g;s/^\.Ah //}' bca
.Ah "Comment"
Comment
.Ah "Substitution"
Substitution
.Ah "Delete"Delete
.Ah "Append, Insert and Change"
Append, Insert and Change
.Ah "List"
List
[root@node1 ~]#