sed高阶用法
作者:互联网
sed高级用法
a 追加
[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa
//向第二行后面追加'hi world'
[root@localhost ~]# sed '2ahi world' test
hello world
jjjd
hi world
aaaaaaa
//过滤包含'world'的行,在其后面追加'hi world'
[root@localhost ~]# sed '/world/ahi world' test
hello world
hi world
jjjd
aaaaaaa
i 插入
[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa
//在第一行插入'hhhh'
[root@localhost ~]# sed '1ihhhh' test
hhhh
hello world
jjjd
aaaaaaa
//过滤包含'jd'的行,插入'hhhh'
[root@localhost ~]# sed '/jd/ihhhh' test
hello world
hhhh
jjjd
aaaaaaa
c 更改
[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa
//将第一行更改为'hi world'
[root@localhost ~]# sed '1chi world' test
hi world
jjjd
aaaaaaa
//过滤包含'world'的行,更改为'HEELO WORLD'
[root@localhost ~]# sed '/world/cHELLO WORLD' test
HELLO WORLD
jjjd
aaaaaaa
y 替换,与's///g'不同的是,y可以将指定的多个单个字符进行替换
[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa
//将文件里的a全部替换为h,h全部替换为a
[root@localhost ~]# sed 'y/ah/ha/' test
aello world
jjjd
hhhhhhh
//可以使用,这种方式进行大小写替换,将文件里的j全部变为大写
[root@localhost ~]# sed 'y/j/J/' test
hello world
JJJd
aaaaaaa
d 删除
[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa
//删除第一行
[root@localhost ~]# vim test
[root@localhost ~]# sed '1d' test
jjjd
aaaaaaa
//删除包含'hello'的行
[root@localhost ~]# sed '/hello/d' test
jjjd
aaaaaaa
D 删除,删除模式空间中直到第一个包含换行符的这部分内容
[root@localhost ~]# cat test
This line is followed by 1 blank line
This line is followed by 2 blank line
This line is followed by 3 blank line
This line is followed by 4 blank line
This is th end
[root@localhost ~]# sed '/^$/{N;/^\n$/d}' test
This line is followed by 1 blank line
This line is followed by 2 blank line
This line is followed by 3 blank line
This line is followed by 4 blank line
This is th end
//换成D试一试
[root@localhost ~]# sed '/^$/{N;/^\n$/D}' test
This line is followed by 1 blank line
This line is followed by 2 blank line
This line is followed by 3 blank line
This line is followed by 4 blank line
This is th end
p 打印 会打印匹配到的行
[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa
//打印第二行,但是会发现,第二行打印了两次,这是因为模式空间和sed的默认输出功能没有区分开,所以要使用-n抑制默认的输出
[root@localhost ~]# sed '2p' test
hello world
jjjd
jjjd
aaaaaaa
[root@localhost ~]# sed -n '2p' test
jjjd
打印以'world'结尾的行
[root@localhost ~]# sed -n '/world$/p' test
hello world
P 打印 打印匹配的行的开端到第一个换行符结束
[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa
[root@localhost ~]# sed -n '/world/{N;P}' test
hello world
//换成p试试
[root@localhost ~]# sed -n '/world/{N;p}' test
hello world
jjjd
n 读取当前匹配行的下一行
[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa
//匹配'world',将world的下一行放入模式空间,然后p打印,并用-n抑制sed的默认打印功能
[root@localhost ~]# sed -n '/world$/{n;p}' test
jjjd
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.
//匹配以'Operator'结尾的行,将其和下一行追加到模式空间,进行替换
[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide /installation Guide\n/g}' test
Consult Section 3.1 in the installation Guide
for a description of the tape drives
available on your system.
//如果在替换过后'Guide'不进行换行的话,效果如下
[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g}' test
Consult Section 3.1 in the installation Guide for a description of the tape drives
available on your system.
h 将模式空间的内容复制到保持空间
H 将模式空间的内容追加到保持空间
g 将保持空间的内容复制到模式空间
G 将保持空间的内容追加到模式空间
[root@localhost ~]# sed '/1/{h;d};/2/g' test
1
11
111
[root@localhost ~]# sed '/1/{h;d};/2/G' test
2
1
22
11
222
111
[root@localhost ~]# sed '/1/{H;d};/2/g' test
1
1
11
1
11
111
[root@localhost ~]# sed '/1/{H;d};/2/G' test
2
1
22
1
11
222
1
11
111
x 交换保持空间和模式空间的内容
[root@localhost ~]# sed '/1/{H;d};/2/x' test
1
2
11
22
111
标签:高阶,用法,sed,test,world,line,root,localhost 来源: https://www.cnblogs.com/zicnotes/p/16699465.html