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	zzz
    26	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
ew

ds

dds
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	123
     2	123
     4	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	345
     5	345
     6	 

在这里插入图片描述

匹配字符串删除行

在这里插入图片描述

在这里插入图片描述

面试题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	234
    13	345
    14	AAA
    15	BBB
    16	ZZZ
    17	QQQ
    18	QQQ
    19	ZZZ
    20	SSS
    21	AAA
    22	DDD
    23	DDD123
    24	345
    25	234

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

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

[root@localhost opt]# cat -n test1 | sed 's/[A-Z]/\l&/g'  
    13	345
    14	aaa
    15	bbb
    16	zzz
    17	qqq
    18	qqq
    19	zzz
    20	sss
    21	aaa
    22	ddd
    23	ddd123
    24	345
    25	234
    26	345
    27	aaa
    28	bbb
    29	zzz
    30	zzz
    31	sss
    32	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自动装机做一个自动化装机做一个自动化部署的脚本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值