linux命令-sed命令使用(3)

sed 的基本使用
sed命令行格式为:
sed [-nefri] 'command' 输入文本

今天咱们 来谈一谈 sed 中的 删除 , 就是 d 这个东东。

[root@myCentOS shell]# cat data1
The quick brown fox jumps over the lazy dog one
The quick brown fox jumps over the lazy dog two
The quick brown fox jumps over the lazy dog three
The quick brown fox jumps over the lazy dog four
The quick brown fox jumps over the lazy dog five
The quick brown fox jumps over the lazy dog six

[root@myCentOS shell]# sed '2 d' data1
The quick brown fox jumps over the lazy dog one
The quick brown fox jumps over the lazy dog three
The quick brown fox jumps over the lazy dog four
The quick brown fox jumps over the lazy dog five
The quick brown fox jumps over the lazy dog six

[root@myCentOS shell]# sed '2,4d' data1
The quick brown fox jumps over the lazy dog one
The quick brown fox jumps over the lazy dog five
The quick brown fox jumps over the lazy dog six

[root@myCentOS shell]# sed '2,$d' data1
The quick brown fox jumps over the lazy dog one

发现 这样删除 只能 删除连续的行, 那么 怎么删除 不连续的行呢?

注意: sed 的 模式匹配特性 也适用于 删除 操作。
[root@myCentOS shell]# sed -r '/dog.*one/d' data1
The quick brown fox jumps over the lazy dog two
The quick brown fox jumps over the lazy dog three
The quick brown fox jumps over the lazy dog four
The quick brown fox jumps over the lazy dog five
The quick brown fox jumps over the lazy dog six

下面 来看看下面 的一个有趣的东西。
[root@myCentOS shell]# cat data2
This is the line number 1.
This is the line number 2.
This is the line number 3.
This is the line number 4.
This is the line number 5.

[root@myCentOS shell]# sed '/2/,/4/d' data2
This is the line number 1.
This is the line number 5.
匹配模式 可以写两个, 这样 sed 就会在第一次匹配成功后,会' 打开' 删除模式,在第二次匹配成功后,会 关闭删除模式, 所以 就会删除 2 3 4 这些行了。
但是要 注意一点
只要sed 编辑器在数据流中 匹配到了开始模式,删除功能就会打开。 这就有可能带来意想不到的结果。

来看看这个例子:
[root@myCentOS shell]# cat data2
This is the line number 1.
This is the line number 2.
This is the line number 3.
This is the line number 4.
This is the line number 5.
This is the line number 1 again.
aaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccc
end end end end end
[root@myCentOS shell]# sed '/1/,/4/d' data2
This is the line number 5.

我本来想 删除1到4行 的内容,
但是结果 5行以后 都没有了。 其实原因 也是和前面分析的一样, 因为在在第5行 下面 , 1 又一次被匹配到了,因此 此时'打开了' 删除功能, 但是4 又没有匹配到,所以没有办法,sed 编辑器 只能删除以后的所有行。 不知道我这样解释, 有没有理解,就是 当第二次匹配到1 的时候, 打开了删除这个功能,但是此后找不到结束模式了,因此把后面的内容全部干掉了。

现在我们在来看看下面问题。
[root@myCentOS shell]# cat data2
This is the line number 1.
This is the line number 2.
This is the line number 3.
This is the line number 4.
This is the line number 5.
This is the line number 1 again.
aaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccc
end end end end end
[root@myCentOS shell]# sed '/1/,/6/d' data2
[root@myCentOS shell]#
[root@myCentOS shell]# sed '/3/,/6/d' data2
This is the line number 1.
This is the line number 2.
[root@myCentOS shell]#

如果 结束的模式,没有匹配到,即6 没有匹配到, sed 编辑 器 则会把 从 开始匹配行,一直删除的文件的末尾。

这里就从 第3行,删除到 尾行。




总结: 简单总结了sed 编辑的删除功能,总的来说 有三种种方式 删除, 第一种指定行数,进行删除,或者指定从某一行到某一行 进行删除。
第二种 删除 方式, 可以通过模式匹配去删除,指定行,这个时候就要考验正则表达式 写的好不好了。第三种 稍微有点复杂 ,其实是对第二种的扩充,通过模式匹配去删除,提供一个 开始 的模式匹配正则表达式,和 结束的模式正则表达式,进行匹配。 这样也可以删除,不过这里 要注意我提到的那些 可能出现的问题。




分享感动,留住快乐。 2017年 10月 07日 星期六 17:50:25 CST ---biaoge  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值