shell sed总结 D指令

本文通过具体实例详细解析了SED命令的工作流程,特别是结合-n选项时的行为差异。深入探讨了如何使用SED命令进行文本处理,包括模式空间与保持空间的交互过程。

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

1、D指令后不加-n也不会打印pattern空间的内容

eg: data文件内容

This is header line
This is first line.
This is second line.

一、指令:sed  'N;h;G;p;D' data

This is header line
This is first line.
This is header line
This is first line.
This is first line.
This is header line
This is first line.
This is second line.
This is first line.
This is header line
This is first line.
This is second line.
This is header line
This is first line.
This is second line.
This is first line.
This is header line
This is first line.
This is second line.

1、读入This is header line

2、N,附加上This is first line.(和第一条数据有\n)

3、h,This is header line

            This is first line.两条数据移动到hold空间

4、G,再将This is header line

                  This is first line.从hold space附加回pattern space(之前pattern space中的那两条还存在,目前一共四条 )即

This is header line
 This is first line.
This is header line

 This is first line. 

5、p,打印

This is header line
 This is first line.
This is header line

 This is first line. 

6、D,删除pattern空间中到\n的数据,也即第一条数据This is header line,剩下

 This is first line.
This is header line

 This is first line. 

7、虽然这条指令没有加sed -n 但是执行的D之后,也不会打印模式空间中剩下的内容了;所以接着再循环从第一条指令开始

8、N,pattern space再附加文件中的This is second line.一共四条

 This is first line.
This is header line
 This is first line. 

This is second line.

9、h,G和上面一样,又复制了一次这四条,一共八条

10、p打印

This is first line.
This is header line
This is first line.
This is second line.
This is first line.
This is header line
This is first line.

This is second line.

11、D删除This is first line. 剩下7条

12、继续N指令,此时没有下一条,执行失败,结束,打印出剩下pattern space中的7条数据(就是没有加sed -n的效果)

This is header line
This is first line.
This is second line.
This is first line.
This is header line
This is first line.

This is second line.

二、指令加-n :

This is header line
This is first line.
This is header line
This is first line.
This is first line.
This is header line
This is first line.
This is second line.
This is first line.
This is header line
This is first line.

This is second line.

前11条与一的情况一样,最后一条加了-n,执行结束是不会把pattern space中的内容打印出来的







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值