[root@192~]# sed '/^\.An/{// 匹配开头为.An的
s/\.An */\ // 替换 .An
\ //结果前面加了换行
@AHEAD=///替换内容
s/"//g // 将“替换为空
s/$/\
/}
' 1
@AHEAD= Major Heading
[root@192~]# sed '/^\.An/{s/\.An */\n@A HEAD = /;s/"//g};s/$/\n/'1
@AHEAD= Major Heading
// &(表示前面匹配到的内容) -r(表示拓展正确表达式) s(替换) g(替换全部的)[root@192~]# sed -r "s/ORA (.*)/O'Reilly &/g"1.An "Major Heading"O'Reilly ORA Associates, Inc.[root@192~]# sed "s/ORA.*/O'Reilly &/g"1.An "Major Heading"O'Reilly ORA Associates, Inc.[root@192~]# sed "s/ORA.*/& 123/g"1.An "Major Heading"ORA Associates, Inc.123[root@192~]# sed "s/ORA/& 123/g"1.An "Major Heading"ORA123 Associates, Inc.
// s(替换) g(替换全部的) \(转义符) &(表示前面匹配到的内容)[root@192~]# cat 1.An "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.[root@192~]# sed 's/UNIX/\\s-2&\\s0/g'1.An "Major Heading"ORA Associates, Inc.
on the \s-2UNIX\s0 Operating System.
// s(替换) g(替换全部的) \(转义符) &(表示前面匹配到的内容)[1-9](表示1到9之间的任意数) *(匹配其前面的任意单个字符任意次) -r(表示拓展正确表达式) 1(表示为第一个括号里匹配到的内容)2(表示为第二个括号里匹配到的内容)[root@192~]# sed 's/See Section [1-9[0-9]*\.[1-9][0-9]*/(&)/g'1.An "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.(See Section 1.4)(See Section 12.9)[root@192~]# sed 's/[1-9[0-9]*\.[1-9][0-9]*/(&)/g'1.An "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section(1.4)
See Section(12.9)[root@192~]# sed -r 's/(See Section) ([1-9[0-9]*\.[1-9][0-9]*)/\1\\fB\2\\fP/g'1.An "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section\fB1.4\fP
See Section\fB12.9\fP
// s(替换) g(替换全部的) -r(表示拓展正确表达式) 1(表示为第一个括号里匹配到的内容)2(表示为第二个括号里匹配到的内容).*(匹配任意单个字符任意次)[root@192~]# cat 1.An "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9one:two
[root@192~]# sed -r 's/(.*):(.*)/\2:\1/g'1.An "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9two:one
[root@192~]# cat 1.An "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9one:two
[root@192~]# sed '/^\.An/d'1// 删除以.An开头的ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9one:two
[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9one:two
[root@192~]# sed '/^.An/d'1// 不加转义符表示单个字符ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9one:two
[root@192~]#
// 行数和a(在该行后面添加内容) 匹配所有开头为内容和a(在该行后面添加内容)[1-9](表示1到9之间的任意数) [root@192~]# sed '1atjf'1.An "Major Heading"
tjf
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9one:two
[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9one:two
[root@192~]# sed '1atjf\ tjf'1// 添加空格需要用\转义符.An "Major Heading"
tjf tjf
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9one:two
[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9one:two
[root@192~]# sed '/^\.An/atjf'1.An "Major Heading"
tjf
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9one:two
[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9one:two
[root@192~]# sed '/^See/atjf'1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
tjf
See Section 12.9
tjf
one:two
[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# sed '/[1-9][0-9]\.[1-9]/atjf'1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
tjf
See Section 12.5
tjf
one:two
// 行数加i(在该行数前添加内容) //内容开头加i(在该行数前添加内容)[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# sed '1itjf'1
tjf
.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# sed '/^\.An/itjf'1
tjf
.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# sed '/^\.An/itjf\
> tjf'1
tjf
tjf
.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
// 行数加c(覆盖该行内容) 开头加c(覆盖该行内容)[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# sed '1ctjf'1
tjf
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# sed '/^\.An/ctjf'1
tjf
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# sed '/^\.An/,/ORA/ctjf'1
tjf
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# sed '1,$ctjf'1
tjf
[root@192~]#
// y(替换全部内容)[root@192~]# cat 1.An "Major Heading"
hAn "Major Heading"ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5one:two
[root@192~]# sed 'y/ajo/123/'1.An "M123r He1ding"
hAn "M123r He1ding"ORA Ass3ci1tes, Inc.3n the UNIX Oper1ting System.
See Secti3n 1.4
See Secti3n 12.9
See Secti3n 12.5
3ne:tw3
[root@192~]# cat 2.An "Comment".An "Substitution".An "Delete".An "Append, Insert and Change".An "List"[root@192~]# sed '/^\.An/{p;s/"//g;s/^\.An //}'2//P为打印.An "Comment"
Comment
.An "Substitution"
Substitution
.An "Delete"
Delete
.An "Append, Insert and Change"
Append, Insert and Change
.An "List"
List
[root@192~]# sed -n '/Comment/{=;p}'2// {=;p} 显示行号1.An "Comment"[root@192~]# cat 2.An "Comment".An "Substitution".An "Delete".An "Append, Insert and Change".An "List"[root@192~]# sed -n '/Comment/{=;p}'21.An "Comment"[root@192~]# sed -n '/Comment/n;p'2// 打印Comment后面的内容.An "Substitution".An "Delete".An "Append, Insert and Change".An "List"[root@192~]# cat 2.An "Comment".An "Substitution".An "Delete".An "Append, Insert and Change".An "List"[root@192~]# sed -r '/Comment/{n;s/\.An (.*)/\.An (\1)/g}'2.An "Comment".An("Substitution").An "Delete".An "Append, Insert and Change".An "List"[root@192~]#
[root@192~]# cat 2.An "Comment".An "Comment".An "Substitution".An "Delete".An "Append, Insert and Change".An "List"[root@192~]# sed '/Comment/{n;/^$/d}'2.An "Comment".An "Comment".An "Substitution".An "Delete".An "Append, Insert and Change".An "List"[root@192~]# sed -n '/Comment/{n;/^$/d};p'2.An "Substitution".An "Delete".An "Append, Insert and Change".An "List"
sed高级用法
[root@localhost ~]# cat abc
Consult Section 3.1in the Owner and Operator
Guide for a description of the tape drives
available on your system.
#改变
[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g}' abc
Consult Section 3.1in the installation Guide for a description of the tape drives
available on your system.
#拆解
[root@localhost ~]# sed -n '/Operator$/{N;p}' abc //匹配以Operator结尾的行 N和匹配行的下一行 p打印
Consult Section 3.1in the Owner and Operator
Guide for a description of the tape drives
[root@localhost ~]# sed -n '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g ;p}' abc //将Owner and Operator\nGuide 变成 installation Guide 因为替换包括了\n(转行) 所以打印时会将两条显示为一条
Consult Section 3.1in the installation Guide for a description of the tape drives
#原文档
[root@localhost ~]# cat abc
Consult Section 3.1in 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 inc luding the Owner and
Operator Guide and the User Gui de.
The Owner and Operator Guide is shipped with your system.
#改变
[root@localhost ~]# sed 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;s/Owner and Operator Guide */installation Guide\n/g}' abc
Consult Section 3.1in 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 inc luding the installation Guide
and the User Gui de.
The installation Guide is shipped with your system.
#拆解
[root@localhost ~]# sed 's/Owner and Operator Guide/installation Guide/g' abc //将Owner and Operator Guide替换为installation Guide
Consult Section 3.1in 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 inc luding the Owner and
Operator Guide and the User Gui de.
The installation Guide is shipped with your system.[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/p' abc //匹配带有Owner的行(此时只有两行)
Consult Section 3.1in the Owner and Operator
Two manuals are provided inc luding the Owner and
[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;p}' abc //将匹配行的下一行追加到模式空间
Consult Section 3.1in the Owner and Operator
Guide for a description of the tape drives
Two manuals are provided inc luding the Owner and
Operator Guide and the User Gui de.[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;p}' abc //将 *\n替换为空格 就变成了两行合并为一行
Consult Section 3.1in the Owner and Operator Guide for a description of the tape drives
Two manuals are provided inc luding the Owner and Operator Guide and the User Gui de.[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;s/Owner and Operator Guide */installation Guide\n/g;p}' abc //将Owner and Operator Guide * 替换为installation Guide\n 改变了Owner and Operator Guide然后后面的换行
Consult Section 3.1in the installation Guide
for a description of the tape drives
Two manuals are provided inc luding the installation Guide
and the User Gui de.
// D 删除模式空间多行的第一行// d 删除模式空间的内容#
原文档
[root@localhost~]# cat abc
This line is followed by1 blank line.
This line is followed by2 blank lines.
This line is followed by3 blank lines.
This line is followed by4 b lank lines.
This is the end.#改变[root@localhost~]# sed '/^$/{N;/^\n$/d}' abc
This line is followed by1 blank line.
This line is followed by2 blank lines.
This line is followed by3 blank lines.
This line is followed by4 b lank lines.
This is the end.#拆解[root@localhost~]# sed -n '/^$/{N;p}' abc //匹配空行将匹配行的下一行追加到模式空间
This line is followed by2 blank lines.
This line is followed by4 b lank lines.[root@localhost~]# sed -n '/^$/{N;/^\n$/d;p}' abc //以换行符开头或结尾的删除(空格偶数删除)
This line is followed by2 blank lines.
This line is followed by4 b lank lines.
#源文件
[root@localhost ~]# cat abc
Here are examples of the UNIX
System. Where UNIX
System appears,it should be the UNIX
Operating System.
#改变
[root@localhost ~]# sed '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D}}' abc
Here are examples of the UNIX Operating
System. Where UNIX Operating
System appears,it should be the UNIX
Operating System.
#拆解
[root@localhost ~]# sed -n '/UNIX$/{N;p}' abc //匹配UNIX结尾的行 将匹配到行的下一行添加到模式空间
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}}' abc // /\nSystem/ 在匹配转行后为System的行
Here are examples of the UNIX
System. Where UNIX[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;p}}' abc //添加Operating
Here are examples of the UNIX Operating
System. Where UNIX[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;P}}' abc //P打印模式空间第一行
Here are examples of the UNIX Operating
[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D}}' abc //删除模式空间的第一行
Here are examples of the UNIX Operating
System. Where UNIX Operating