SECTION 25 sed进阶(一)

sed进阶

sed的模式空间和保持空间
sed之所以能以行为单位的编辑或修改文本,其原因在于使用了两个空间:模式空间和保持空间

模式空间:可以想象成流水线,数据之间在它上面进行加工处理
保持空间:可以想象成仓库,在进行处理数据时,作为数据的暂存区域

正常情况下,不使用某些特定的高级命令,保持空间不会被用到
sed正常情况下,将处理的行读入模式空间,脚本中的sed命令就一条一条处理,直到脚本完成,然后该行被输出,模式空间清空。接着读取下一行到模式空间,重复执行刚才的动作,直到文件被处理完毕。
但有些特殊的情况下,使用保持空间会有意想不到的效果,后文中介绍

多行命令

在前文的sed编辑器基础命令中,所有的sed命令都是针对单行数据执行操作,它会基于换行符将数据分行。有时需要对跨多行的数据进行特定操作。

例如:查找短语linux system administrators group,它有可能出现在两行中,每行包含其中一部分,如果用sed基础命令,可能无法发现这种被分开的短语。

sed编辑器包含了三种可用于处理多行文本的特殊命令:

1.N:将数据流中的下一行加进来创建一个多行组(multiline group)来处理
2.D:删除多行组中的一行
3.P:打印多行组中的一行

next命令
1.单行next命令
通常sed在移动到数据流中的下一行文本之前,会在当前行上执行完所有定义好的命令
next命令n会告诉sed移动到数据流的下一文本行,而不用重新回到当前行
例如删除下例中的第一个空行

]# cat data1.txt 
this is the header line

this is the data line

this is the last line

常规只能删除所有空行

]# sed '/^$/d' data1.txt 
this is the header line
this is the data line
this is the last line

使用n命令

]# sed '/header/{n;d}' data1.txt 
this is the header line
this is the data line

this is the last line

]# sed '/dat1/{n;s/dat2/DAT2/}' data1.txt 
this is the header line
this is the dat1 Lion
king is the DAT2 line
this is Lion king line

把匹配行下一行移动到模式空间后,对数据进行处理,然后输出

2.合并文本行
单行next命令会将数据流中匹配行的下一文本行移动到sed编辑器的工作空间(模式空间),其他行直接被输出
多行next命令会将下一行添加到模式空间中已有的文本后,多行next命令(N),当当前行执行完命令后,跳过下一行,开始处理第三行内容,简单理解为两行处理

]# cat data1.txt 
this is the header line
this is the dat1 line
this is the dat2 line
this is the dat3 line
this is the dat4 line
this is the last line
]# sed '/the/{N;s/\n/<==>/;s/dat/DAT/}' data1.txt 
this is the header line<==>this is the DAT1 line
this is the DAT2 line<==>this is the dat3 line
this is the DAT4 line<==>this is the last line

匹配到the,然后进行通过N将下一行进行s处理

示例1

]# cat data1.txt 
this is the header line
this is the dat1 Lion
king is the dat2 line
this is Lion king line
this is the dat4 line
this is the last line

]# sed 'N;s/Lion.king/LION KING/' data1.txt 
this is the header line
this is the dat1 Lion
king is the dat2 line
this is LION KING line
this is the dat4 line
this is the last line

示例2

]# cat data2.txt 
this is the header line

this is the dat1 Lion
king is the dat2 line
this is Lion king line
this is the dat4 line
this is the last line
]# sed 'N;s/Lion.king/LION KING/' data2.txt 
this is the header line

this is the dat1 LION KING is the dat2 line
this is LION KING line
this is the dat4 line
this is the last line

示例3

]# cat data3.txt 
this is the header line

this is the dat1 Lion
king is the dat2 line
this is Lion king line
this is the dat4 line
this is Lion king line
]# sed 'N;s/Lion.king/LION KING/' data3.txt 
this is the header line

this is the dat1 LION KING is the dat2 line
this is LION KING line
this is the dat4 line
this is Lion king line

大N模式的Next问题还是不少,例如无法处理第二三行之间存在的匹配项(示例1),无法处理末行(示例3)

]# sed 's/Lion king/LION KING/;N;s/Lion.king/LION KING/' data3.txt 
this is the header line

this is the dat1 LION KING is the dat2 line
this is LION KING line
this is the dat4 line
this is LION KING line

可以通过增加命令解决末行问题

多行删除命令
单行删除命令d,和N命令一起使用,需要小心
多行命令D,它只删除模式空间中的第一行,该命令会删除到换行符(含换行符)为止的所有字符,匹配和N使用

]# cat data1.txt 

this is the header line
this is the dat1 Lion
]# sed '/^$/{N;/header/D}' data1.txt 
this is the header line
this is the dat1 Lion

多行打印命令
多行打印命令P只打印模式空间的第一行

保持空间

模式空间(pattern space)是一块活跃的缓冲区,默认的sed编辑器在模式空间使用命令
sed还有一块称作保持空间(hold space)的缓冲区,在处理模式空间中的某些行时,可以用保持空间保存一些行,进行操作
常用命令:

命令 描述
h 将模式空间复制到保持空间
H 将模式空间附加到保持空间
g 将保持空间复制到模式空间
G 将保持空间附加到模式空间
x 交换模式空间和保持空间内容

]# cat data1.txt 

this is the header line
this is the dat1 Lion
king is the dat2 line
]# sed -n '/header/{h;n;p;g;p}' data1.txt 
this is the dat1 Lion
this is the header line

排除命令
排除(negate)命令感叹号!,让原来匹配的行命令不起作用,让原来不匹配的行命令起作用
格式:[[address]!]command
对!前面标记所匹配的地址或模式不执行command,而不匹配的行执行command

]# sed -n '/header/p' data1.txt 
this is the header line
]# sed -n '/header/!p' data1.txt 

this is the dat1 Lion
king is the dat2 line
this is Lion king line
this is the dat4 line
this is Lion king line

反转文本

]# sed -n '{1!G;h;$p}' data1.txt 
this is Lion king line
this is the dat4 line
this is Lion king line
king is the dat2 line
this is the dat1 Lion
this is the header line
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值