欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 36 - shell之sed

36 - shell之sed

2024/10/25 10:32:33 来源:https://blog.csdn.net/m0_74149099/article/details/139863028  浏览:    关键词:36 - shell之sed

sed

grep就是查找文本当中的内容,扩展正则表达式。

一、sed

1.1、sed的定义

sed是一种流编辑器,一次处理一行内容。

如果只是展示,会放在缓冲区(模式空间),展示结束,会从模式空间把结果删除。

一行一行处理,处理完当前行,才会处理下一行,直到文件末尾。

1.2、sed的命令格式和操作选项:

sed -e ‘操作符’ -e ‘操作符’ 文件1 文件2

-e表示可以跟多个操作符,只有一个操作,-e可以省略,这里的-e是指定操作。

sed -e ‘操作符1;操作符2’ 文件1 文件2

1.2.1、选项:

-e:用于执行多个操作

-f :在脚本中定义好了操作符,然后根据脚本内容的操作符对文件进行操作。

-i :直接修改目标文件(慎用)

-n:仅显示script处理后的结果(不加-n,sed会有两个输出结果,加了-n就会把默认输出屏蔽,只显示一个结果)。

1.2.2、操作符:

p:打印结果

r:扩展正则表达式

s:替换,替换字符串

c:替换,替换行

y:替换,替换单个字符串,多个字符替换必须和替换内容的字符长度保持一致。

d:删除,删除行

a:增加,在指定行的下一行插入内容

[root@localhost opt]# cat -n test1 | sed '/ddd/a aa'

i:增加 ,在指定行上一行插入内容

[root@localhost opt]# cat -n test1 | sed '/ddd/i aa'

r:插入文本内容

[root@localhost opt]# cat -n test1 | sed '/ddd/r test2' 

$a:在最后一行下一行,插入新的内容

[root@localhost opt]# cat -n test1 | sed '$a sdsddsds12343' 

$i:在最后一行上一行,插入新的内容

[root@localhost opt]# cat -n test1 | sed '$i sdsddsds12343' 

$r:在最后一行下一行,插入文本内容

[root@localhost opt]# cat -n test1 | sed '$r test2' 

1.2.3、打印功能:

1.2.3.1、寻址打印

行号打印

[root@localhost opt]# sed -n '=;p' test1

打印第四行

[root@localhost opt]# sed -n '4p' test1

打印最后一行

[root@localhost opt]# sed -n '$p' test1

显示行号

[root@localhost opt]# cat -n test1 | sed -n 'p' 
1.2.3.2、行号范围打印

打印第二到最后一行

[root@localhost opt]# sed -n '2,$p' test1

打印第二,最后一行

[root@localhost opt]# sed -n '2p;$p' test1
123
ddd

打印奇数行和偶数行

打印偶数行,n跳过第一行

[root@localhost opt]# cat -n test1 | sed -n 'n;p'

打印奇数行,n跳过第一行

[root@localhost opt]# cat -n test1 | sed -n 'p;n' 

n的作用,跳过一行,打印下一行。

1.2.4、文本内容进行过滤

过滤并打印包含o的行,/过滤内容/

[root@localhost opt]# cat -n test1 | sed -n '/z/p'         ##/过滤内容/13	zzz26	zzz

使用正则表达式对文本内容进行过滤

[root@localhost opt]# cat /etc/passwd | sed -n '/^a/p' 
adm:x:3:4:adm:/var/adm:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin

从指定行开始,打印到第一个以bash为结尾的行

[root@localhost opt]# cat /etc/passwd | sed -n '3,/bash$/p'

扩展正则表达式

image-20240621101128472
[root@localhost opt]# cat /etc/passwd | sed -rn '/(99:){2,}/p' 
nobody:x:99:99:Nobody:/:/sbin/nologin

要么以root开头,要么以bash结尾

[root@localhost opt]# cat /etc/passwd | sed -rn '/\broot|bash\b/p' 

面试题1:

如何免交互删除文本内容。不删除文件

sed 删除文件内容

[root@localhost opt]# sed -i 'd' test2
[root@localhost opt]# cat test2
[root@localhost opt]# 

cat /dev/null 删除文件内容

[root@localhost opt]# cat test2
12
ewdsdds
f
d
fd
f
g[root@localhost opt]# cat /dev/null > test2
[root@localhost opt]# cat test2

sed的删除操作

删除指定行

[root@localhost opt]# cat -n test1 | sed -n '3d;p' 1	1232	1234	345

删除25到最后一行

[root@localhost opt]# cat -n test1 | sed -n '25,$d;p' 

删除指定x行

[root@localhost opt]# cat -n test1 | sed -n 'x!d;p' 

除了4-6行,其他全部删除

[root@localhost opt]# cat -n test1 | sed -n '4,6!d;p' 4	3455	3456	 

在这里插入图片描述

匹配字符串删除行

在这里插入图片描述

在这里插入图片描述

面试题2:

如何免交互的方式删除空行:

在这里插入图片描述

[root@localhost opt]# grep -v "^$" test1[root@localhost opt]# cat test1 | tr -s "\n"[root@localhost opt]# sed '/^$/d' test1

s替换字符串----加g全部替换

sed -n ‘s/需要替换/替换成/gp’

[root@localhost opt]# cat /etc/passwd | sed -n 's/root/test/p'  
test:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/test:/sbin/nologin
[root@localhost opt]# cat /etc/passwd | sed -n 's/root/test/gp'  
test:x:0:0:test:/test:/bin/bash
operator:x:11:0:operator:/test:/sbin/nologin
[root@localhost opt]# cat -n test1 | sed -n 's/^/#/gp'  #     1	123#     2	#     3	#     4	123
[root@localhost opt]# cat -n test1 | sed -n '4p;5p' | sed -n 's/^/#/gp'  #     4	123                ##打印后注释#     5	
[root@localhost opt]# cat -n test1 | sed -n '4s/^/#/gp;6s/^/#/gp'  #     4	123#     6	123

首字母变大写------加g是全部

[root@localhost opt]# cat -n test1 | sed 's/[a-z]/\u&/' 

u&转换首字母大写的特殊符号,\转义符。

[root@localhost opt]# cat -n test1 | sed 's/[a-z]/\u&/g'  12	23413	34514	AAA15	BBB16	ZZZ17	QQQ18	QQQ19	ZZZ20	SSS21	AAA22	DDD23	DDD12324	34525	234

首字母变小写—加g是全部

l&转换首字母大写的特殊符号,\转义符。

[root@localhost opt]# cat -n test1 | sed 's/[A-Z]/\l&/g'  13	34514	aaa15	bbb16	zzz17	qqq18	qqq19	zzz20	sss21	aaa22	ddd23	ddd12324	34525	23426	34527	aaa28	bbb29	zzz30	zzz31	sss32	aaa

整行替换:

[root@localhost opt]# cat -n test1 | sed '/123/c dn zhen de shuai'
[root@localhost opt]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 | sed '/IPADDR=192.168.168.10/c /IPADDR=192.168.168.100'
TYPE=Ethernet
DEVICE=ens33
ONBOOT=yes
BOOTPROTO=static
/IPADDR=192.168.168.100

y,单字符替换

[root@localhost opt]# cat -n test1 | sed 'y/abc/678/'

r:插入文本内容

[root@localhost opt]# cat -n test1 | sed '/ddd/r test2' 

$a:在最后一行下一行,插入新的内容

[root@localhost opt]# cat -n test1 | sed '$a sdsddsds12343' 

$i:在最后一行上一行,插入新的内容

[root@localhost opt]# cat -n test1 | sed '$i sdsddsds12343' 

$r:在最后一行下一行,插入文本内容

[root@localhost opt]# cat -n test1 | sed '$r test2' 

使用sed对字符串和字符的位置进行互换。

[root@localhost opt]# echo chengqianshuai | sed -r 's/(cheng)(qian)(shuai)/\3\1\2/'
shuaichengqian

字符位置互换----使用.任意单字符代替

[root@localhost opt]# echo cheng | sed -r 's/(.)(.)(.)(.)(.)/\4\5\3\1\2/'
ngech

在这里插入图片描述

面试题3:

筛选安装版本号

ant-1.9.7.jar
ant-launcher-1.9.7.jar
antlr-2.7.7.jar
antlr-runtime-3.4.jar
aopalliance-1.0.jar
archaius-core-0.7.6.jar
asm-5.0.4.jar
aspectjweaver-1.9.5.jar
bcpkix-jdk15on-1.64.jar
bcprov-jdk15-1.46.jar
bcprov-jdk15on-1.64.jar
checker-compat-qual-2.5.5.jar
[root@localhost opt]# cat test1.txt | sed -r 's/(.*)-(.*)(\.jar)/\2/'
1.9.7
1.9.7
2.7.7
3.4
1.0
0.7.6
5.0.4
1.9.5
1.64
1.46
1.64
2.5.5
[root@localhost opt]# cat test1.txt | egrep -o "\b([0-9][.][0-9][0-9])|\b([0-9][.][0-9][.][0-9])"
1.9.7
1.9.7
2.7.7
0.7.6
5.0.4
1.9.5
1.64
1.46
1.64
2.5.5

打印指定时间内的日志:

[root@localhost opt]# tail /var/log/messages | sed -n '/Jun 21 13:01:01/,/Jun 21 14:00:01/p'

sed的主要作用是对文本的内容进行增删改查

其中最好用的,最强大的是改和增。

作业:

使用脚本的形式,结合sed命令,把pxe自动装机做一个自动化装机做一个自动化部署的脚本。
)(.jar)/\2/’
1.9.7
1.9.7
2.7.7
3.4
1.0
0.7.6
5.0.4
1.9.5
1.64
1.46
1.64
2.5.5


[root@localhost opt]# cat test1.txt | egrep -o “\b([0-9][.][0-9][0-9])|\b([0-9][.][0-9][.][0-9])”
1.9.7
1.9.7
2.7.7
0.7.6
5.0.4
1.9.5
1.64
1.46
1.64
2.5.5


打印指定时间内的日志:

[root@localhost opt]# tail /var/log/messages | sed -n ‘/Jun 21 13:01:01/,/Jun 21 14:00:01/p’


sed的主要作用是对文本的内容进行增删改查其中最好用的,最强大的是改和增。# 作业:使用脚本的形式,结合sed命令,把pxe自动装机做一个自动化装机做一个自动化部署的脚本。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com