Unix Sed Tutorial 2 : Delete File Lines Using Address and Patterns

In the previous sed tutorial we discussed about Unix sed command basics and printing lines 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.

  • “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

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.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.

1. Delete Nth Line

‘Nd’ deletes the Nth line and prints the other lines.

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.

For example, 3d deletes 3rd line and prints other lines as shown below.

$ 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.

2. Delete Starting from 3rd line and every 2nd line from there.

$ 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.

3. Delete from 4th to 8th line from file.

$ 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.

4. Delete the last line from input.

$ 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

5. Delete the line which matches the given pattern from input.

For example, the below command deletes the line which matches with “Sysadmin”.

$ 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

6. Deletes the line from which matches the given pattern to end of the file.

$ sed '/Website/,$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)

7. Deletes the line from which matches the given pattern and 2lines next to that.

$ 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.

8. Delete blank Line from a file using sed

You can also remove blank lines with sed. The following sed example shows how to use sed and remove blank 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值