sed高级应用
作者:互联网
sed高级应用
sed命令的语法
单个行地址
[address]command
使用大括号进行分组使其用于同一个地址
[line-address]command
替换
替换命令语法
[address]s/pattern/replacement/flages
flags可以是:
n 1到512之间的数字,表示文本模式第n次出现的情况进行替换
g 对匹配的结果进行全局替换
p 打印替换后的内容
w 将模式空间的内容写到文件file中
#自定义分隔符
[root@localhost ~]# echo '/usr/local/src' | sed 's#/usr/local/src#/user/local/src#'
/user/local/src
将第二个制表符 替换为*号
[root@localhost ~]# cat abc
column1 column2 column3 column4 //此处空格为制表符 (tab键)
[root@localhost ~]# sed 's/\t/*/'2 abc //命令行无法输入tab 使用\t 和制表符同义
column1 column2*column3 column4
将第二个制表符替换为换行
[root@localhost ~]# sed 's#\t#\
#2' abc
column1 column2
column3 column4
[root@localhost ~]# sed 's#\t#\n#2' abc
column1 column2
column3 column4
将上面的转换为下面的
[root@localhost ~]# cat aaa
.Ah Major Heading
[root@localhost ~]# sed '/^.Ah/{ //先匹配以.Ah开头的行
> s/\.Ah */\
> \
> @A HEAD = / //将.Ah* 替换为 @A HEAD =
> s/"//g //引号替换为空
> s/$/\ // $替换为空行
> /
> } ' aaa
//此处有空行
@A HEAD = Major Heading
//此处有空行
[root@localhost ~]# cat aaa
.Ah Major Heading
ORA Associates, Inc.
[root@localhost ~]# sed -r "s/ORA (.*)/O'Reilly &/g" aaa
.Ah Major Heading
O'Reilly ORA Associates, Inc.
[root@localhost ~]# cat aaa
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
[root@localhost ~]# sed 's/UNIX/\\s-2&\\s0/g' aaa //将UNIX 替换为 \s-2UNIX\s0
.Ah Major Heading
ORA Associates, Inc.
on the \s-2UNIX\s0 Operating System
[root@localhost ~]# cat aaa
.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' aaa
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
(See Section 1.4)
(See Section 12.9)
[root@localhost ~]# cat aaa
.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' aaa //匹配:冒号前后的替换位置
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
second:first
two:one
删除
删除空行
[root@localhost ~]# cat test
aaa
aaa
aaa
[root@localhost ~]# sed '/^$/d' test
aaa
aaa
aaa
删除以指定开头的
[root@localhost ~]# cat test
.sp
.bp
.nf
.fi
[root@localhost ~]# sed '/^.sp/d' test
.bp
.nf
.fi
[root@localhost ~]# sed '/^.nf/d' test
.sp
.bp
.fi
追加
追加(a)
插入(i)
更改(c)
第一行后面添加abc
[root@localhost ~]# sed '1a abc' aaa
.Ah Major Heading
abc
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two
在abc前面添加空格
[root@localhost ~]# ^C
[root@localhost ~]# cat aaa
.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 '1a " abc"' aaa //将空格引起来
.Ah Major Heading
" abc"
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '1a / abc' aaa //转义
.Ah Major Heading
/ abc
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^See/a 123' aaa //在匹配指定行 后面添加
.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 '/See.*[1-9][0-9]\.[0-9]/a abc' aaa //在12.9后面添加abc
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
abc
first:second
one:two
插入
[root@localhost ~]# cat aaa
.Ah Major Heading
[root@localhost ~]# cat aaa
.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 '1i xixi' aaa //在第一行插入xixi
xixi
.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 ~]# cat aaa
.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/i xixi' aaa //匹配ORA开头的 插入xixi (会使原本的在此的行 变成下一行)
.Ah Major Heading
xixi
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# cat aaa
.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 '/^first/i xixi\ //插入两行
hehe' aaa
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
xixi
hehe
first:second
one:two
更改
[root@localhost ~]# cat aaa
.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/,/on/c abc' aaa //将.Ah开头到第一匹配到的on 更改为abc
abc
See Section 1.4
See Section 12.9
first:second
one:two
转换
[root@localhost ~]# cat test
abc abc xxx yyy abc
abc abc xxx yyy abc
abc abc xxx yyy abc
[root@localhost ~]# sed 'y/abc/123/' test //将所有abc转换为123
123 123 xxx yyy 123
123 123 xxx yyy 123
123 123 xxx yyy 123
[root@localhost ~]# sed '1y/abc/123/' test //将第一行的abc转换为123
123 123 xxx yyy 123
abc abc xxx yyy abc
abc abc xxx yyy abc
[root@localhost ~]# sed '1y/acc/123/' test //转换是将匹配到的单个字符转换为对应的 b没有匹配到不会进行转换
1b2 1b2 xxx yyy 1b2
abc abc xxx yyy abc
abc abc xxx yyy abc
打印
[root@localhost ~]# cat test
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
[root@localhost ~]# sed '/^\.Ah/{p}' test //因为本身会显示一遍 p打印会显示一遍所以有两行
.Ah "Comment"
.Ah "Comment"
.Ah "Substitution"
.Ah "Substitution"
.Ah "Delete"
.Ah "Delete"
[root@localhost ~]# cat test
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
[root@localhost ~]# sed '/^\.Ah/{p;s/"//g;s/\.Ah/xixi/}' test //去掉引号 并将.Ah替换为xixi 并打印
.Ah "Comment"
xixi Comment
.Ah "Substitution"
xixi Substitution
.Ah "Delete"
xixi Delete
[root@localhost ~]# sed -n '/Comment/{=;p}' test //打印前显示行号
1
.Ah "Comment"
[root@localhost ~]# cat test
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
[root@localhost ~]# sed -r '/Comment/{n;s/\.Ah (.*)/\.Ah (\1)/g}' test //匹配Comment的下一行 (.*)为(/1) .Ah不变 为(.*)加上括号
.Ah "Comment"
.Ah ("Substitution")
.Ah "Delete"
sed高级操作
模式空间:工作站
保持空间:仓库
N 将匹配行的下一行追加到模式空间
#原文档
[root@localhost ~]# cat abc
Consult Section 3.1 in 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.1 in 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.1 in 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.1 in the installation Guide for a description of the tape drives
#原文档
[root@localhost ~]# cat abc
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 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.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 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.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 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.1 in 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.1 in 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.1 in 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.1 in 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 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 b lank lines.
This is the end.
#改变
[root@localhost ~]# sed '/^$/{N;/^\n$/d}' abc
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 b lank lines.
This is the end.
#拆解
[root@localhost ~]# sed -n '/^$/{N;p}' abc //匹配空行将匹配行的下一行追加到模式空间
This line is followed by 2 blank lines.
This line is followed by 4 b lank lines.
[root@localhost ~]# sed -n '/^$/{N;/^\n$/d;p}' abc //以换行符开头或结尾的删除(空格偶数删除)
This line is followed by 2 blank lines.
This line is followed by 4 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
源文件
[root@localhost ~]# cat abc
1
2
11
22
111
222
#变化
[root@localhost ~]# sed -n '/1/{h;d};/2/{G};p' abc
2
1
22
11
222
111
[root@localhost ~]# sed -n '/1/{h;d};p' abc //将模式空间的1追加到保持空间 并删除模式空间的1
2
22
222
标签:abc,Ah,Section,高级,sed,应用,root,localhost 来源: https://www.cnblogs.com/soap-bubble/p/16699411.html