SED series: row or pattern match delete a specific row

Source: Internet
Author: User

"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

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.