"P" command prints the buffer (remember to use-n option with "P")
The "D" command is just opposite, which is a for deletion. ' d ' would delete the pattern space buffer and immediately starts the next cycle.
Syntax:
# sed ' ADDRESS ' d filename
# sed/pattern/d filename
The
Let us first creates Thegeekstuff.txt file, which is 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. Productivity (Too Many technologies to explore, not much time available)
8. Website Design
9. Software development
10.windows-sysadmin, reboot etc.
Example 1: delete nth row
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 the 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. Productivity (Too Many technologies to explore, not much time available)
8. Website Design
9. Software development
10.windows-sysadmin, reboot etc.
Example 2: Delete every other row starting from the third line
$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 line 4th to line 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 line
$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
$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
Example 5: Row matching Delete
$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
$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
$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
Example 6: From match line to end line
$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. Productivity (Too Many technologies to explore, 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. Productivity (Too Many technologies to explore, not much time available)
Example 7: Delete the matching line and the next two lines
$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 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.
SED series: row or pattern match delete a specific row