“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. C++ Language

    C++ Language Reference https://msdn.microsoft.com/en-us/library/3bstk3k5(v=vs.120).aspx

  2. lvm使用总结-转

    由于安装的kvm需要扩容,研究了下lvm的使用. LVM(Logical Volume Manager)逻辑卷管理器,做法是将物理分区通过软件组合未一个独立的大磁盘(VG,卷组),然后把这个大磁盘分成 ...

  3. Linux体系结构(二): Linux系统层次

    前一节内容大概介绍了一下Linux的历史由来,各大Linux发行厂商都是基于GNU项目下的所有开源软件,来构建各自己的Linux发行版本,一个完整的Linux发行版本大概可以分为以下四个层次结构:   ...

  4. C++ map详解

    1.什么是mapmap是一个键值对容器.在处理一对一数据是,很有用. 2.map数据结构的特点map内部自建一颗红黑树,这棵树具有对数据自动排序的功能,因此,map内的数据都是按key的值排好序的. ...

  5. 016. asp.net的验证控件

    RequiredFileldValidator: 检查某个字段是否输入; 空值检查 主要属性: ControlToValidate:要验证的控件 ErrorMessage:错误提示信息 Compare ...

  6. pam

    http://www.linux-pam.org/ sag(the linux-pam system administrators' guide)翻译(以下是网上别人的翻译,参考一下) http:// ...

  7. C#中的List<string>泛型类示例

    在C#代码中使用一系列字符串(strings)并需要为其创建一个列表时,List<string>泛型类是一个用于存储一系列字 符串(strings)的极其优秀的解决办法.下面一起有一些Li ...

  8. Java语言编码规范(Java Code Conventions)

    Java语言编码规范(Java Code Conventions) 名称 Java语言编码规范(Java Code Conventions) 译者 晨光(Morning) 简介 本文档讲述了Java语 ...

  9. 解决Oracle 11g在用EXP导出时,空表不能导出

    实践结论可行,看"2.解决办法" 一.问题原因:     11G中有个新特性,当表无数据时,不分配segment,以节省空间     1.insert一行,再rollback就产生 ...

  10. ASP.NET MVC 页面调整并传递参数

    转自:http://blog.csdn.net/zhensoft163/article/details/7174661 使用过ASP.NET MVC的人都知道在MVC中页面后台中常用的页面跳转方法有几 ...