sed高级应用
命令 | |
---|---|
a | 在当前行下面插入文本 |
i | 在当前行上面插入文本 |
c | 把选定的行改为新的文本 |
d | 删除选择的行 |
D | 删除模式空间的第一行 |
y | 表示把一个字符翻译为另外的字符(但是不用于正则表达式) |
n | 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令 |
N | 追加下一个输入行到模式空间后面并在二者间嵌入一个新行,改变当前行号码 |
p | 打印模式空间的行 |
P(大写) | 打印模式空间的第一行 |
g | 获得保持空间的内容,并替代当前模式空间中的文本 |
G | 获得保持空间的内容,并追加到当前模式空间文本的后面 |
h | 拷贝模式空间的内容到内存中的保持空间 |
H | 追加模式空间的内容到内存中的保持空间 |
x | 表示互换模式空间中的文本和保持空间中的文本 |
1. sed基本命令
# 删除第3行数据
[root@localhost ~]# cat test_1
123
456
789
321
654
987
[root@localhost ~]# sed '3d' test_1
123
456
321
654
987
转义符可以是 “\”
、“#”
、或“!”
[root@localhost ~]# echo '/usr/local/src' | sed 's#/usr/local/src#/user/local/src/#'
/user/local/src/
\t
表示制表符,将制表符替换为>
[root@localhost ~]# echo 'Column1 Column2 Column3 Column4' > test_1
[root@localhost ~]# cat test_1
Column1 Column2 Column3 Column4
[root@localhost ~]# sed 's/\t/>/2' test_1
Column1 Column2>Column3 Column4
\n
表示换行
[root@localhost ~]# sed 's/\t/\n/2' test_1
Column1 Column2
Column3 Column4
将.Ah "Major Heading"
替换为@A HEAD = Major Heading
[root@localhost ~]# cat test_2
.Ah "Major Heading"
# 第一种方法:
[root@localhost ~]# sed '/^\.Ah/{
s/\.Ah */\
\
@A HEAD = /
s/"//g
s/$/\
/
} ' test_2
@A HEAD = Major Heading
# 第二种方法:
[root@localhost ~]# sed '/^\.Ah/{s/\.Ah */@A HEAD = /;s/"//g}' test_2
@A HEAD = Major Heading
输出效果为:O' Reilly ORA Associates, Inc.
-r
表示用正则表达式
[root@localhost ~]# echo 'ORA Associates, Inc.' >> test_2
[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.
[root@localhost ~]# sed -r "s/ORA (.*)/O' Reilly &/g" test_2
.Ah "Major Heading"
O' Reilly ORA Associates, Inc.
输出效果为:on the \s-2UNIX\s0 Operating System.
[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
[root@localhost ~]# sed 's/UNIX/\\s-2&\\s0/g' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the \s-2UNIX\s0 Operating System.
“&”
可以引用匹配的内容
[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
[root@localhost ~]# sed 's/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/g' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
(See Section 1.4)
(See Section 12.9)
[root@localhost ~]# sed -r 's/(See Section) ([1-9][0-9]*\.[1-9][0-9]*)/\1\\fB\2\\fp/g' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section\fB1.4\fp
See Section\fB12.9\fp
交换内容位置
[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed -r 's/(.*):(.*)/\2:\1/g' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
second:first
two:one
删除内容(d)
[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^.Ah/d' test_2
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
追加(a)
[root@localhost ~]# sed '1aabc' test_1
Column1 Column2 Column3 Column4
abc
[root@localhost ~]# sed '1a abc' test_1
Column1 Column2 Column3 Column4
abc
[root@localhost ~]# sed '/^See/a 123' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
123
See Section 12.9
123
first:second
one:two
[root@localhost ~]# sed '/See.*[1-9][0-9]\.[0-9]/a123' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
123
first:second
one:two
插入(i)
[root@localhost ~]# sed '1ihello' test_1
hello
Column1 Column2 Column3 Column4
[root@localhost ~]# sed '1 i hello' test_1
hello
Column1 Column2 Column3 Column4
[root@localhost ~]# sed '/^\.Ah/ihello' test_2
hello
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
更改(c)
[root@localhost ~]# cat test_1
Column1 Column2 Column3 Column4
[root@localhost ~]# sed '1chello' test_1
hello
[root@localhost ~]# sed '1chello' test_1
hello
[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/ORA/c \hello' test_2
.Ah "Major Heading"
hello
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^\.Ah/,/on /chello' test_2
hello
See Section 1.4
See Section 12.9
first:second
one:two
转换(y)
[root@localhost ~]# sed '1y/ajo/123/' test_2
.Ah "M123r He1ding"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
下一行(n)
# 在匹配到内容的下一行进行更改
[root@localhost ~]# sed -r '/Associates/{n;s/on the (.*)/on the (\1)/g}' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the (UNIX Operating System.)
See Section 1.4
See Section 12.9
first:second
one:two
2. 多行模式空间
追加下一行(N)
[root@localhost ~]# cat test
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
[root@localhost ~]# sed -n '/Operator$/{N;p}' test
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
[root@localhost ~]# sed '/3.1/{N;s/3.1/3.2/g;s/Owner and Operator\nGuide/installation Guide/g}' test
Consult Section 3.2 in the installation Guide for a description of the tape drives
available on your system.
[root@localhost ~]# cat test
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
Look in the Owner and Operator Guide shipped with your system.
Two manuals are provided including the Owner and
Operator Guide and the User Guide.
The Owner and Operator Guide is shipped with your system.
[root@localhost ~]# sed 's/Owner and Operator Guide/Installation Guide/g' test
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
Look in the Installation Guide shipped with your system.
Two manuals are provided including the Owner and
Operator Guide and the User Guide.
The Installation Guide is shipped with your system.
[root@localhost ~]# sed -n 's/Owner and Operator Guide/Installation Guide/g;/Owner/p' test
Consult Section 3.1 in the Owner and Operator
Two manuals are provided including the Owner and
[root@localhost ~]# sed -n 's/Owner and Operator Guide/Installation Guide/g;/Owner/{N;s/ *\n/ /;p}' test
Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives
Two manuals are provided including the Owner and Operator Guide and the User Guide.
[root@localhost ~]# sed 's/Owner and Operator Guide/Installation Guide/g;/Owner/{N;s/ *\n/ /;s/Owner and Operator Guide */Installation Guide\n/g}' test
Consult Section 3.1 in the Installation Guide
for a description of the tape drives
available on your system.
Look in the Installation Guide shipped with your system.
Two manuals are provided including the Installation Guide
and the User Guide.
The Installation Guide is shipped with your system.
多行删除(D)
[root@localhost ~]# cat test
This line is followed by 1 blank line.
This line is followed by 2 blank lines.
This line is followed by 3 blank lines.
This line is followed by 4 blank lines.
[root@localhost ~]# sed -n '/^$/{N;/^\n$/d;p}' test
This line is followed by 2 blank lines.
This line is followed by 4 blank lines.
[root@localhost ~]# sed '/^$/{N;/^\n$/d}' test
This line is followed by 1 blank line.
This line is followed by 2 blank lines.
This line is followed by 3 blank lines.
This line is followed by 4 blank lines.
多行打印(P)
[root@localhost ~]# cat test
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX
Operating System.
[root@localhost ~]# sed -n '/UNIX$/p' test
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX
[root@localhost ~]# sed -n '/UNIX$/{N;p}' test
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX
Operating System.
[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{p}}' test
Here are examples of the UNIX
System. Where UNIX
[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;p}}' test
Here are examples of the UNIX Operating
System. Where UNIX
[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D;p}}' test
Here are examples of the UNIX Operating
System. Where UNIX Operating
替换&追加(g、G)
[root@localhost ~]# cat test
1
2
11
22
111
222
[root@localhost ~]# sed '/1/{h;d};/2/G' test
2
1
22
11
222
111