Unix Sed Tutorial: Delete File Lines Using Address and Patterns
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.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.
相關文章
- Retrieve deleted files on Unix / Linux using File Descriptors [ID 444749.1]deleteLinux
- Using UTL_FILE Package To Perform File I/O (UNIX) And Basic FAQ_44307.1PackageORM
- Entity Framework Tutorial Basics(25):Delete Single EntityFrameworkdelete
- Leaflet入門:新增點線面並匯入GeoJSON資料|Tutorial of Leaflet: Adding Points, Lines, Polygons and Import GeoJSON FileJSONGoImport
- python unix :No such file or directoryPython
- [Shell] Monitor filesystem usage & delete expire filedelete
- 5.Monitor filesystem usage & delete expire filedelete
- Using Create directory & UTL_FILE in OracleOracle
- recover database using backup control fileDatabase
- SED單行指令碼快速參考(Unix 流編輯器)指令碼
- using dbms_file_transfer transportable tablespace between asmASM
- GoldenGate replication using a data definition file and DEFGEN utilityGo
- How to copy a datafile from ASM to a file system not using RMANASM
- Unable to View Chinese Font When Using Tools>Copy FileView
- Setup SSL using .PFX file on nginx/apache2NginxApache
- React PatternsReact
- How to serve uncommon file in WebDAV using IIS in Win ServerWebServer
- FROM ASM Migrating to FILE SYSTEM Using RMAN(三)ASM
- Lines演示程式
- Telephone Lines S
- SUID and SGID: -rwsr-xr-x Unix file permissionUI
- No model,no patterns ,then no frameworkFramework
- Digital PatternsGit
- 解決 ALL MIRROR URLS ARE NOT USING FTP, HTTP[S] OR FILE 問題FTPHTTP
- Concurrency Patterns in GoGo
- Recreate stdby Control File When dbf Are On ASM And Using OMF-734862.1ASM
- JavaScript TutorialJavaScript
- Glob Patterns匹配模式使用模式
- Patterns and Frameworks (my first topic)Framework
- How To Restore 12cR1DB to New Host File System using RMAN(一)REST
- 類Unix流編輯器sed線上極速入門 第三部分
- 類Unix流編輯器sed線上極速入門 第二部分
- TECH: Getting a Stack Trace from a CORE file on Unix (Doc ID 1812.1)
- sed 工具
- 文字處理的金剛鑽 —— sed (GNU/sed)
- 類Unix流編輯器sed線上極速入門 第一部分
- Install 11.2/12.1 DB/Client in Silent Mode without Using Response File _885643.1client
- Step-By-Step Guide To Create Physical Standby On Normal File System For ASM Primary using RMANGUIIDEORMASM