本文共 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.
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.
‘Nd’ deletes the Nth line and prints the other lines.
sed ‘Nd’ filename
As per sed methodology,
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.
$ 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.
$ sed '4,8d' thegeekstuff.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.3. Hardware9. Software Development10.Windows- Sysadmin, reboot etc.
$ 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
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
$ 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)
$ 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.
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/