热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

linux命令sed命令使用(3)

sed的基本使用sed命令行格式为:sed[-nefri]command输入文本今天咱们来谈一谈sed中的删除,就是d这个东东。[rootmyCentO
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 '2d' 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 




推荐阅读
author-avatar
手机用户2502871803
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有