ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

sed高阶用法

2022-09-16 13:04:00  阅读:248  来源: 互联网

标签:高阶 用法 sed test world line root localhost


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有