Sed series: deletes a specific row by matching the row or pattern.
"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
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. productitime (Too technologies to recommend e, not much time available)
8. Website Design
9. Software Development
10. Windows-Sysadmin, reboot etc.
Example 1: Delete row n
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.
$ 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. productitime (Too technologies to recommend e, not much time available)
8. Website Design
9. Software Development
10. Windows-Sysadmin, reboot etc.
Example 2: delete each row from the third row
$ 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.
$
Example 3: delete rows from 4th to 8th
$ 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.
$ 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.
$ Sed '4, 8' d thegeekstuff.txt
1. Linux-Sysadmin, Scripting etc.
2. Databases-Oracle, mySQL etc.
3. Hardware
9. Software Development
10. Windows-Sysadmin, reboot etc.
Example 4: Delete the last row
$ 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. productitime (Too technologies to recommend e, not much time available)
8. Website Design
9. Software Development
$ 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. productitime (Too technologies to recommend e, not much time available)
8. Website Design
9. Software Development
Example 5: Delete row matching
$ 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. productitime (Too technologies to recommend e, not much time available)
8. Website Design
9. Software Development
$ 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. productitime (Too technologies to recommend e, not much time available)
8. Website Design
9. Software Development
$ 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. productitime (Too technologies to recommend e, not much time available)
8. Website Design
9. Software Development
$
Example 6: from matching row to end row
$ Sed '/Website Design/, $ 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. productitime (Too technologies to recommend e, not much time available)
$ Sed '/Website Design/, $ '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. productitime (Too technologies to recommend e, not much time available)
Example 7: Delete the matching row and the last two rows
$ 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.
$ Sed '/Storage/, + 2 'd 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.
Example 8: delete empty rows
$ 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. productitime (Too technologies to recommend e, not much time available)
8. Website Design
9. Software Development
10. Windows-Sysadmin, reboot etc.