In the previous sed tutorial we discussed about Unix sed command basics and printing lines from a file using sed address and patterns.

In this article, let us review how to delete lines from a file using address and patterns with 8 awesome examples.

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

Syntax for ADDRESSES and PATTERNS given in the printing is applicable for deletion also, except -n option. (-n only to suppress printing pattern buffer, can be used with “p” command )

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. Delete Nth Line

‘Nd’ deletes the Nth line and prints the other lines.

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.

For example, 3d deletes 3rd line and prints other lines as shown below.

$ 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. Delete Starting from 3rd line and every 2nd line from there.

$ 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. Delete from 4th to 8th line from file.

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

4. Delete the last line from input.

$ 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. Delete the line which matches the given pattern from input.

For example, the below command deletes the line which matches with “Sysadmin”.

$ 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. Deletes the line from which matches the given pattern to end of the file.

$ sed '/Website/,$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. Deletes the line from which matches the given pattern and 2lines next to that.

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

8. Delete blank Line from a file using sed

You can also remove blank lines with sed. The following sed example shows how to use sed and remove blank lines.

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

Unix Sed Tutorial 2 : Delete File Lines Using Address and Patterns相关推荐

  1. Unix Sed Tutorial 5: How To Execute Multiple Sed Commands

    Question: Is it possible for me to combine multiple sed commands? Can I combine two sed commands and ...

  2. git delete file remote

    2019独角兽企业重金招聘Python工程师标准>>> git delete file remote Use commands : $ git rm /path to file na ...

  3. Android Unable to delete file: build\intermediates\manifests\full\debug\AndroidManifest.xm

    莫名其妙的报这个错,还遇到不止一次... Unable to delete file: build\intermediates\manifests\full\debug\AndroidManifest ...

  4. Android studio 报错 :Unable to delete file

    Error:Execution failed for task ':app:clean'. > Unable to delete file: D:\android\workspace\As1.5 ...

  5. Gradle:unable to delete file

    android studio编译打包时出错 Error:Exception failed for task':accountms:clean'. >Unable to delete file: ...

  6. Java删除文件(delete file in java)

    Java中,可用File.delete()删除一个文件,调用该方法后将返回一个布尔类型的值,true表示删除成功,false则表示删除失败. 本篇文章,将删除"H:\\temp\\style ...

  7. delete file$ recovery----惜分飞

    最近遭遇几次有人因为对oracle不太理解,由于各种情况下,删除了file$中的部分记录,从而使得该文件之后的文件都丢失,使得数据库出现各种异常情况.这里演示了可以重启数据库的情况下两种常见的删除fi ...

  8. unix终端上的delete end home 键怎么不管用

    毕设已经过了一个月了,由于要运行并行程序,老师帮我申请了一个学校信息系统中心的hp并行机的一个终端,使unix系统的.一开始用vi编辑真是麻烦,什么键盘上的delete键,回退键等等都起不了效果,搞得 ...

  9. java delete file 失败_file.delete()无法删除文件的原因及解决方法

    file.delete()无法删除文件的原因及解决方法 发布时间:2020-05-06 09:41:00 来源:亿速云 阅读:756 作者:小新 今天小编给大家分享的是file.delete()无法删 ...

最新文章

  1. 在express.js上启用HTTPS
  2. axios mysql_axios的简单使用
  3. 线性结构--离散存储 链表讲解
  4. adf4351使用_使用ADF绑定创建视图对象行CreateInsert操作
  5. 安卓+servlet+MySql 查询+插入(汉字乱码解决)
  6. hashset如何检查重复_如何使用 C# 中的 HashSet
  7. 没有配置默认路由_网络路由选择原理
  8. Gse v0.40.0 发布,Go 高性能分词,增加更多常用 API
  9. springMVC+Mybatis实现查询分页
  10. VOSviewer软件研究热点分析
  11. 移动魔盒cm201-2原厂备份固件hitool刷机包hi3798mv300h
  12. 同比增长和环比增长的区别
  13. 又是被打败的一天 (记招商银行笔试)
  14. 【7.13】代码源 -【饿饿 饭饭】【路径计数2】【函数求和】
  15. 【youcans 的 OpenCV 例程200篇】176.图像分割之均值漂移算法 Mean Shift
  16. 面试官这么表现就有戏?不一定哦!
  17. 计算机毕业设计之全网独家Spark租房爬虫数据分析与推荐系统 租房大数据 租房app 租房数据分析 租房爬虫 房源推荐系统 房源数据分析 房源可视化
  18. Android 引入高德地图 SDK
  19. JavaScript如何打开exe文件
  20. 2023年湖南中专单招报名流程

热门文章

  1. redis 登录客户端命令
  2. 《Rework》摘录及感想(转载)
  3. 解决下载MP3等文件自动播放完美解决方法
  4. CAU SUBMISSION TO DCASE 2021 TASK6: TRANSFORMER FOLLOWED BY TRANSFER LEARNING FOR AUDIO CAPTIONING
  5. 如何在Spring Boot中使用TDD写出高质量的接口
  6. c语言怎么创建循环链表,纯C语言实现循环双向链表创建,插入和删除
  7. Java+SSM驾校管理系统(含源码+论文+答辩PPT等)
  8. 揭秘快速提升alexa排名的18种有效方法-#来秀美#
  9. iPhone开发之SQLite
  10. 好看的电影,好心分享【2】