SHELL-----sed关于 p、d、a、c、i 模式详解

本文详细介绍Sed行编辑器的基本概念、命令格式及使用方法。包括显示、删除、添加、更改和插入等操作,通过实例展示如何处理文本文件,如显示特定行、删除行和修改内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sed 行编辑器

1.sed(stream editor):

*    用来操作纯 ASCII 码的文本

*    Sed 一次处理一行内容处理时,把当前处理的行存储在临时缓冲区中,称之为“模式空间”(pattern space)

*    可以指定仅仅处理哪些行,Sed 符合模式条件的处理,不符合条件的不予处理

*     处理完成之后把缓冲区的内容送往屏幕

*     接着处理下一行,这样不断重复,直到文件末尾

 

2. sed命令格式;
sed [参数] ‘命令’ file

 

3.Sed 对字符的处理:

p     显示,将某个选择的数据打印显示。通常 p 会与参数 sed -n 一起执行
d     删除,显示模式空间删除指定行后的内容,不会对原文件数据删除
a     添加,a 的后面可以接字符串,该字符串会在当前指定行的下一行出现
c     更改, c 的后面可以接字符串,该字符串可以取代 n1,n2 之间的行
i     插入, i 的后面可以接字符串,该字符串会在当前指定行的上一行出现

 

二.用法详解:

1) p:

[root@server19 mnt]# cat -n /etc/fstab 
    1	
    2	#
    3	# /etc/fstab
    4	# Created by anaconda on Wed May  7 01:22:57 2014
    5	#
    6	# Accessible filesystems, by reference, are maintained under '/dev/disk'
    7	# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    8	#
    9	UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /     xfs     defaults        1 1



[root@server19 mnt]# sed -n '/\:/p' /etc/fstab   ##显示含有:的行(:需要转译)
# Created by anaconda on Wed May  7 01:22:57 2014



[root@server19 mnt]# sed -n '/^#/p' /etc/fstab   ##显示以#开头的行
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#


[root@server19 mnt]# sed -n '/^#/!p' /etc/fstab   ##显示不以#开头的行(!)

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /          xfs     defaults        1 1


[root@server19 mnt]# sed -n '2,6p' /etc/fstab     ##显示2-6行
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'


[root@server19 mnt]# sed -n '2,6!p' /etc/fstab      ##不显示2-6行的内容

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /             xfs     defaults        1 1



[root@server19 mnt]# sed -n '2p;6p' /etc/fstab    ##显示第2行和第6行【以分号为分隔符】
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'






 

 

2)d:

root@server19 mnt]# sed '/^UUID/d' /etc/fstab  ##删除以UUID开头的行

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#


[root@server19 mnt]# sed '/^#/d' /etc/fstab   ##删除以#开头的行

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /       xfs     defaults        1 1


[root@server19 mnt]# sed '/^$/d' /etc/fstab   ##删除空行
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /     xfs     defaults        1 1


[root@server19 mnt]# sed '1,4d' /etc/fstab    ##删除1-4行
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /      xfs     defaults        1 1

    

 

 

 

3)a:

 

[root@server19 mnt]# cat hello.sh 
hello

[root@server19 mnt]# sed '/hello/aworld' hello.sh 
hello
world


全文替换:
[root@server19 mnt]# sed 's/hello/hello world/g' hello.sh 
hello world


全文替换加上换行:
[root@server19 mnt]# sed 's/hello/hello\nworld/g' hello.sh 
hello
world

    

 

4)c:

[root@server19 mnt]# sed '/hello/chello world' hello.sh 
hello world

 

 

 

5)i:

[root@server19 mnt]# sed '/hello/iworld\nwestos' hello.sh 
world          ##默认加在最上面
westos
hello

 

 

 

 

 

 

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值