博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unix Sed Tutorial 2 : Delete File Lines Using Address and Patterns
阅读量:2218 次
发布时间:2019-05-08

本文共 4362 字,大约阅读时间需要 14 分钟。

In the previous sed tutorial we discussed about  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.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.3. Hardware4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. Software Development10.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.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. Software Development10.Windows- Sysadmin, reboot etc.

2. Delete Starting from 3rd line and every 2nd line from there.

$ sed '3~2d' thegeekstuff.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.4. Security (Firewall, Network, Online Security etc)6. Cool gadgets and websites8. Website Design10.Windows- Sysadmin, reboot etc.

3. Delete from 4th to 8th line from file.

$ sed '4,8d' thegeekstuff.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.3. Hardware9. Software Development10.Windows- Sysadmin, reboot etc.

4. Delete the last line from input.

$ sed '$d' thegeekstuff.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.3. Hardware4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. 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.txt2. Databases - Oracle, mySQL etc.3. Hardware4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. Software Development

6. Deletes the line from which matches the given pattern to end of the file.

$ sed '/Website/,$d' thegeekstuff.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.3. Hardware4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. 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.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.3. Hardware4. Security (Firewall, Network, Online Security etc)8. Website Design9. Software Development10.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.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.3. Hardware4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. Software Development10.Windows- Sysadmin, reboot etc.

转载地址:http://guffb.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>
Leetcode C++《热题 Hot 100-47》236.二叉树的最近公共祖先
查看>>
Leetcode C++《热题 Hot 100-48》406.根据身高重建队列
查看>>
《kubernetes权威指南·第四版》第二章:kubernetes安装配置指南
查看>>
Leetcode C++《热题 Hot 100-49》399.除法求值
查看>>
Leetcode C++《热题 Hot 100-51》152. 乘积最大子序列
查看>>
Leetcode C++ 《第181场周赛-1》 5364. 按既定顺序创建目标数组
查看>>
Leetcode C++ 《第181场周赛-2》 1390. 四因数
查看>>
阿里云《云原生》公开课笔记 第一章 云原生启蒙
查看>>
阿里云《云原生》公开课笔记 第二章 容器基本概念
查看>>
阿里云《云原生》公开课笔记 第三章 kubernetes核心概念
查看>>
阿里云《云原生》公开课笔记 第四章 理解Pod和容器设计模式
查看>>
阿里云《云原生》公开课笔记 第五章 应用编排与管理
查看>>
阿里云《云原生》公开课笔记 第六章 应用编排与管理:Deployment
查看>>
阿里云《云原生》公开课笔记 第七章 应用编排与管理:Job和DaemonSet
查看>>
阿里云《云原生》公开课笔记 第八章 应用配置管理
查看>>
阿里云《云原生》公开课笔记 第九章 应用存储和持久化数据卷:核心知识
查看>>
linux系统 阿里云源
查看>>