Sed series: deletes a specific row by matching the row or pattern.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.