美文网首页funny生物信息
linux下使用sed 命令来删除指定行

linux下使用sed 命令来删除指定行

作者: 夜空最亮的9星 | 来源:发表于2018-08-09 14:41 被阅读29次
“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. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

例子1:删除第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. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

例2:从第三行开始,每隔一行删除

$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:删除从第4行到第8行

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

例4:删除最后一行

$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

例5:行匹配删除

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

✨ 例6:从匹配行到末尾行 ✨

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

例7:删除匹配行和之后两行

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

例8:删除空行

$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 -i '/setenforc/,+2d' filename.txt  #记得加 -i 才可以对文件真正更改

若想动态的删除包含某个串的行可以这么做.

sed -in '/'$变量名'/d' 文件名

相关文章

  • linux下使用sed 命令来删除指定行

    例子1:删除第n行 sed ‘Nd’ filenameAs per sed methodology,It read...

  • Linux系列开坑记(三)-流处理器sed

    今天我们聊一聊Linux 下sed 命令,sed是linux非常强大的命令,可以对数据进行替换,删除,新增等操作。...

  • sed 通配符应用

    " . " :表示匹配一个字符 " .* " :表示匹配任意字符 sed删除指定行 sed通配符使用 sed多...

  • 【sed】文本处理样例

    输出指定行 使用 sed sed命令下批量替换文件内容 格式: 示例: 把当前目录下lishan.txt里的sha...

  • MongoDB导入文件夹(内含json和bson文件)

    使用mongo命令将数据库删除: mongo命令: 导入(指定文件夹)数据: linux命令: windows命令:

  • 临时随笔sed/awk

    sed替换指定行内容 sed 命令行格式 脚本格式 基本操作命令 行定位 操作命令 实例 替换命令 高级操作命令 ...

  • Linux命令-sed

    删除:d命令 $ sed ‘2d’example —–删除example文件的第二行。 $ sed ‘2,$d’e...

  • LINUX sed命令的使用

    LINUX sed命令的使用 命令格式 sed常用命令 sed替换标记 sed元字符集 已匹配字符串标记& ⼦串匹...

  • sed增删文本的指定行

    1、删除文档的第一行 sed -i '1d' 2、删除文档的最后一行 sed -i '$d' 3、在文档指定行中增...

  • linux删除大文件的前n行

    linux删除大文件的前n行 在数据挖掘中我们经常会增量更新训练日志,需要删除前n行的过期数据,直接用sed命令比...

网友评论

    本文标题:linux下使用sed 命令来删除指定行

    本文链接:https://www.haomeiwen.com/subject/luzqbftx.html