“p” command prints the buffer (remember to use -n option with “p”) 
“d” command is just opposite, its for deletion. ‘d’ will delete the pattern space buffer and immediately starts the next cycle.

Syntax: 
# sed 'ADDRESS'd filename 
# sed /PATTERN/d filename

Let us first creates thegeekstuff.txt file that will be used in all the examples mentioned below. 
# cat thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
10.Windows- Sysadmin, reboot etc.

例子1:删除第n行 
sed ‘Nd’ filename 

As per sed methodology, 
It reads the first line and places in its pattern buffer. 
Check whether supplied command is true for this line, if true, deletes pattern space buffer and starts next cycle. i.e Read next line. 
If supplied command doesnt true, as its normal behaviour it prints the content of the pattern space buffer.

$ sed 3d thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development

10.Windows- Sysadmin, reboot etc.

例2:从第三行开始,每隔一行删除 
$sed 3~2d thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
4. Security (Firewall, Network, Online Security etc) 
6. Cool gadgets and websites 
8. Website Design 
10.Windows- Sysadmin, reboot etc.

$

例3:删除从第4行到第8行 
$sed 4,8d thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
9. Software Development 
10.Windows- Sysadmin, reboot etc. 
$sed '4,8d' thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
9. Software Development 
10.Windows- Sysadmin, reboot etc. 
$sed '4,8'd thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
9. Software Development

10.Windows- Sysadmin, reboot etc.

例4:删除最后一行 
$sed '$'d thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
$sed '$d' thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design

9. Software Development

例5:行匹配删除 
$sed /Sysadmin/d thegeekstuff.txt 

2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
$sed '/Sysadmin/d' thegeekstuff.txt 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
$sed '/Sysadmin/'d thegeekstuff.txt 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development

$

例6:从匹配行到末尾行 
$sed '/Website Design/,$d' thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
$sed '/Website Design/,$'d thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites

7. Productivity (Too many technologies to explore, not much time available)

例7:删除匹配行和之后两行 
$sed '/Storage/,+2d' thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
8. Website Design 
9. Software Development 
10.Windows- Sysadmin, reboot etc. 
$sed '/Storage/,+2'd thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
8. Website Design 
9. Software Development

10.Windows- Sysadmin, reboot etc.

例8:删除空行 
$sed '/^$/d' thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
10.Windows- Sysadmin, reboot etc.

sed 使用 删除匹配行的更多相关文章

  1. Linux sed命令删除指定行

    一.删除包含匹配字符串的行## 删除包含baidu.com的所有行sed -i '/baidu.com/d' domain.file 二.删除匹配行及后所有行## 删除匹配20160229的行及后面所 ...

  2. VIM技巧(2)-删除匹配行

    VIM技巧(2)-删除匹配行 代码如下: * @Company:中国股份有限公司 * @author ymzhao (也有zyyang的) * @Date: Jan 22, 2014 11:25:29 ...

  3. sed常见用法,删除匹配行的上2行,下3行

    删除匹配的下一行到最后一行 [root@test200 ~]# cat test a b c d e f [root@test200 ~]# sed '/c/{p;:a;N;$!ba;d}' test ...

  4. sed命令:删除匹配行和替换

    删除以a开头的行 sed -i '/^a.*/d' tmp.txt -i 表示操作在源文件上生效.否则操作内存中数据,并不写入文件中.在分号内的/d表示删除匹配的行 替换匹配行: sed -i 's/ ...

  5. 拾遗:Vim 批量删除匹配到的行

    删除包含特定字符的行 g/pattern/d (全局删除匹配行) ,5g/pattern/d (删除第1-5行里的匹配行) 删除不包含指定字符的行 v/pattern/d g!/pattern/d ( ...

  6. Linux sed正则匹配删除整行

    原文内容: [root@10 tmp]# more test.log 2019-12-01 09:09:02 Failed 2019-12-01 09:12:02 Failed 2019-12-01 ...

  7. sed系列:行或者模式匹配删除特定行

    “p” command prints the buffer (remember to use -n option with “p”) “d” command is just opposite, its ...

  8. Linux之sed:删除某行以及替换

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed ...

  9. sed 删除最后几行 和删除指定行 awk使用

    sed 删除最后几行 和删除指定行   转载原文链接:http://blog.51cto.com/lspgyy/1305489 sed 想删除文件中的指定行,是可以用行号指定也可以用RE来匹配的. 删 ...

随机推荐

  1. ADC 分辨率和精度的区别

    分辨率和精度这两个,经常拿在一起说,才接触的时候经常混为一谈.对于ADC来说,这两样也是非常重要的参数,往往也决定了芯片价格,显然,我们都清楚同一个系列,16位AD一般比12位AD价格贵,但是同样是1 ...

  2. Nginx-搭建https服务器

    先看Nginx中的配置 server { listen ; ssl on; ssl_certificate /usr/local/nginx/conf/任意证书名.crt; ssl_certifica ...

  3. JS之变量的运算

    js变量的特点: 1.区分大小写,这是与html及css最大的不同: 2.弱变量.通过var进行定义,无明确的数据类型. 第一部分 字符型 对于字符型的数据,常用的操作为字符的转换.字符的操作 1.字 ...

  4. Python_Day_5装饰器、字符串格式化、序列化、内置模块、生成器、迭代器之篇

    一.装饰器 为什么要用装饰器??? 在实际的开发环境中应遵循开发封闭原则,虽然在这个原则是用的面向对象开发,但也适用于函数式编程,简单地说,它规定已经实现的功能代码不是允许修改的,但是可以被扩展: 封 ...

  5. python_Day1_基础知识开篇

    一.python安装 1)windows上同时安装python2.0和python3.0配置 (1)在python官网下载windows版本python2.0和python3.0安装包 官网地址:ht ...

  6. phpcmsv9如何实现添加栏目时不在首页内容区显示只在导航栏显示

    之前王晟璟一直使用PHPCMSV9系统建过自己的个人门户网站,同时也建立了一个其他类型的网站,感觉非常不错,我不得不说PHPCMSV9的功能非常齐全,非常强大. 但有一点时常让王晟璟感到很烦脑,那就是 ...

  7. WCF学习心得------(二)设计和实现服务协定

    设计和实现服务协定 创建服务协定—WCF术语 消息 消息是一个独立的数据单元,它可能由几个部分组成,包括消息正文和消息头. 服务 服务是一个构造,它公开一个或多个终结点,其中每个终结点都公开一个或多个 ...

  8. 屏幕取色工具推荐 ColorPix

    很好用的一个屏幕取色工具,方便套页面时,在图片上取色. 用鼠标指到取色未知,按CTRL+C,就可复制16进制的颜色值. 下载地址:http://files.cnblogs.com/zjfree/Col ...

  9. Python基础(二) —— 字符串、列表、字典等常用操作

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 二.三元运算 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为 ...

  10. 【多线程】Future和FutureTask以及CompletionService

    Future接口 此接口主要用于: 代表异步计算的执行结果: 用于可取消的task:(比使用interrupt实现取消要方便 ) FutureTask类 FutureTask是Future的一个实现类 ...