Unix Sed Tutorial: Delete File Lines Using Address and Patterns
来源:互联网 发布:mac地址转换 编辑:程序博客网 时间:2024/05/21 11:31
Unix Sed Tutorial: Delete File Lines Using 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 )
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.
- Unix Sed Tutorial: Delete File Lines Using Address and Patterns
- Unix Sed Tutorial: Printing File Lines using Address and Patterns
- Unix Sed教程: Append, Insert, Replace, and Count File Lines
- Address Lines and Buses
- Sed - An Introduction and Tutorial
- Create, Read, Write, Copy, Move and Delete a Text File using C# and VB.NET
- Create, Read, Write, Copy, Move and Delete a Text File using C#
- C++ Delete Directory and File
- Introduction: Using diff and patch (tutorial)
- Exclude all lines match a pattern using ls and grep
- Core Graphics Tutorial Part 3: Patterns and Playgrounds
- unix find command tutorial and sample code
- How to add, modify, or delete registry subkeys and values by using a registration entries (.reg) file
- Linux shell: Sed - An Introduction and Tutorial by Bruce Barnett
- Sed - An Introduction and Tutorial by Bruce Barnett
- Design Patterns Video Tutorial
- Unix Sed
- Write a method to print the last K lines of an input file using C++
- 如何在activity中动态的设置TextView上字体的颜色
- zoj 1610 Count the Colors
- Unix Sed Tutorial: Printing File Lines using Address and Patterns
- 操作系统启动过程——硬件自检+系统引导+系统加载+系统登录
- linux内存管理之sys_brk实现分析【一】
- Unix Sed Tutorial: Delete File Lines Using Address and Patterns
- 阅读xapian源代码得到的启发
- 在iOS中加入iAds广告
- cp 参数
- No manual entry for pthread_create
- Sed Tips and Tricks
- VS2010 菜单字体模糊的解决方案
- DbUtility数据库操作
- WordToHtml