“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.

转载于:https://www.cnblogs.com/eustoma/p/5452794.html

sed系列:行或者模式匹配删除特定行相关推荐

  1. 骚操作,如何使用 sed 命令从文本文件中删除特定行?

    您可以使用 Vim 或 Nano 等文本编辑器,输入文件并删除所需的行,但是,这种方法不适合使用 bash 脚本进行自动化. 相反,您可以使用强大的 sed 命令行编辑器并删除符合特定条件的行,当然, ...

  2. Shell:用sed命令删除特定行

    一.sed命令 Linux sed 命令是利用脚本来处理文本文件. sed 可依照脚本的指令来处理.编辑文本文件. 此处用-i参数实现 -i : 直接在文件上编辑 (edit files in pla ...

  3. pandas获得指定行_pandas取dataframe特定行/列

    转自他人博客:https://blog.csdn.net/weixin_39586825/article/details/111758506 1.按列取.按索引/行取.按特定行列取 import nu ...

  4. java向Word模板中替换书签数据,插入图片,插入复选框,插入Word中表格的行数据,删除表格行数据

    java向Word模板中替换书签数据,插入图片,插入复选框,插入Word中表格的行数据,删除表格行数据 使用插件:spire.doc 创建工具类,上代码: import com.spire.doc.D ...

  5. python删除特定的几行数据_Python:分成几行,并根据搜索删除特定行 - python

    我有一个csv文件,如下所示,并以我的一点python知识,我试图将其内容分为基于" sec"的行作为开始字段,并删除包含sip:+ 99 *,sip:+ 88 *的字段的特定行, ...

  6. python df删除特定行_pandas.DataFrame删除/选取含有特定数值的行或列实例

    1.删除/选取某列含有特殊数值的行 import pandas as pd import numpy as np a=np.array([[1,2,3],[4,5,6],[7,8,9]]) df1=p ...

  7. python 删除特定行数据_怎么用 Python 做数据分析实例

    01 生成数据表 第一部分是生成数据表,常见的生成方法有两种,第一种是导入外部数据,第二种是直接写入数据. Excel 中的文件菜单中提供了获取外部数据的功能,支持数据库和文本文件和页面的多种数据源导 ...

  8. python怎么删除特定文件_如何使用python从文件中删除特定行?

    代码是:from datetime import datetime,time from csv import reader with open('onlyOnce.txt', 'r+') as fon ...

  9. 如何在python中使用正则表达式从多行字符串中删除特定字符

    我有一个如下所示的多行字符串: st = '''emp:firstinfo\n:secondinfo\nthirdinfo''' print(st) 我正在尝试做的是从我的字符串中跳过第二个':',并 ...

最新文章

  1. 风控算法干货交流来了!
  2. 走马观花AutoML
  3. Android事件流程详解
  4. Java高级补充(Socket服务端、客户端)、Handler类、安卓Socket传数据、WebView
  5. .NET 6新特性试用 | PeriodicTimer
  6. [ZJOI2014] 璀璨光华(bfs建图 + dfs搜索)
  7. 万字长文:对账系统从入门到精通(建议收藏)
  8. OpenCV学习笔记(二):3种常用访问图像中像素的方式
  9. 【学习笔记】路由算法与路由协议:RIP协议与距离向量算法、OSPF协议与链路状态算法、BGP协议
  10. LaTeX详细安装步骤和简明教程
  11. 车牌识别SDK支持Android、ios
  12. 支招功能最强人工智能围棋软件_AI智能棋盘全新上线,围棋的奥秘原来在这里?...
  13. JSP无法加载静态资源:Failed to load resource: the server responded with a status of 404 ()
  14. 少年群侠传服务器维护时间,少年群侠传开服表
  15. 如何购买银行理财子公司的理财产品?
  16. ETF操作实战记录:2022-2-24
  17. Ubuntu的以太网卡找不到解决方法
  18. 通达信接口服务如何委托下单?
  19. 使用RPA有什么好处
  20. prescan8.5 百度网盘下载链接及安装过程

热门文章

  1. Swagger2 生成API文档时泛型总是显示不出来的问题
  2. Docker-Windows 2008 R2安装使用
  3. HTTP 如何传输大文件
  4. 跨域:Response to preflight request doesn t pass access control check: No Access-Control-Allow-Origin
  5. dockerfile制作镜像及k8s中应用
  6. 帮助 Medium 阅读时间达到 2600 年的技术栈
  7. 在 ML2 中配置 OVS vlan network - 每天5分钟玩转 OpenStack(136)
  8. IOS开发中的几种设计模式
  9. Apache Shiro SessionManager配置详解.
  10. STP/RSTP/MSTP经典分析与对比