在本文中,让我们通过 8 个示例来了解如何使用地址和模式从文件中删除行。
“p”命令打印缓冲区(记住使用 -n 选项和“p”)
“d”命令正好相反,用于删除。'd'将删除模式空间缓冲区并立即开始下一个循环。
Syntax:
# sed 'ADDRESS'd filename
# sed /PATTERN/d filename
打印中给出的 ADDRESSES 和 PATTERNS 的语法也适用于删除,但 -n 选项除外。(-n 仅用于抑制打印模式缓冲区,可与“p”命令一起使用)
让我们首先创建 geekstuff.txt 文件,该文件将在下面提到的所有示例中使用。
# cat tiamotxt.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.删除第N行
'Nd' 删除第 N 行并打印其他行。
sed 'Nd' 文件名
根据 sed 方法,
它读取第一行并将其放置在其模式缓冲区中。
检查此行提供的命令是否为真,如果为真,则删除模式空间缓冲区并开始下一个循环。即阅读下一行。
如果提供的命令不为真,则作为其正常行为,它会打印模式空间缓冲区的内容。
例如,3d 删除第 3 行并打印其他行,如下所示。
$ sed 3d tiamotxt.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. 删除从第 3 行开始和从那里开始的每 2 行。
$ sed '3~2d' tiamotxt.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. 从文件中删除第 4 行到第 8 行。
$ sed '4,8d' tiamotxt.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
9. Software Development
10.Windows- Sysadmin, reboot etc.
4. 从输入中删除最后一行。
$ sed '$d' tiamotxt.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. 从输入中删除与给定模式匹配的行。
例如,下面的命令删除与“Sysadmin”匹配的行。
$ sed /Sysadmin/d tiamotxt.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. 删除与给定模式匹配的行到文件末尾。
$ sed '/Website/,$d' tiamotxt.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. 删除与给定模式匹配的行和旁边的 2 行。
$ sed '/Storage/,+2d' tiamotxt.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. 使用 sed 从文件中删除空行
您还可以使用 sed 删除空行。下面的 sed 示例展示了如何使用 sed 和删除空行。
$ sed '/^$/d' tiamotxt.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.