linux系统删除指定的行(sed命令)

1、使用vim创建测试数据 a.txt

复制代码
[root@linuxprobe test]# cat a.txt
1 w e t
2 s f h
3 z c g
4 e a g
5 a f w
6 k h d
7 w f r
复制代码

 

2、删除指定的行

复制代码
[root@linuxprobe test]# sed '3d' a.txt ##删除第三行
1 w e t
2 s f h
4 e a g
5 a f w
6 k h d
7 w f r
复制代码
[root@linuxprobe test]# sed '1,3d' a.txt ## 删除1到3行
4 e a g
5 a f w
6 k h d
7 w f r
[root@linuxprobe test]# sed '1d;3d' a.txt ## 删除第一行和第三行
2 s f h
4 e a g
5 a f w
6 k h d
7 w f r
复制代码
[root@linuxprobe test]# sed '/s/d' a.txt ##删除匹配s的行
1 w e t
3 z c g
4 e a g
5 a f w
6 k h d
7 w f r
[root@linuxprobe test]# sed '/w/d' a.txt ##删除匹配w的行
2 s f h
3 z c g
4 e a g
6 k h d
复制代码
复制代码
[root@linuxprobe test]# sed '/^5/d' a.txt ## 删除以5开头的行
1 w e t
2 s f h
3 z c g
4 e a g
6 k h d
7 w f r
[root@linuxprobe test]# sed '/^[35]/d' a.txt ## 删除以3或者5开头的行
1 w e t
2 s f h
4 e a g
6 k h d
7 w f r
复制代码
复制代码
[root@linuxprobe test]# sed '/h$/d' a.txt ## 删除以h结尾的行
1 w e t
3 z c g
4 e a g
5 a f w
6 k h d
7 w f r
[root@linuxprobe test]# sed '/[hw]$/d' a.txt ## 删除以h或者w结尾的行
1 w e t
3 z c g
4 e a g
6 k h d
7 w f r
复制代码
复制代码
[root@linuxprobe test]# cat a.txt  ##使用vim编辑器重新编辑测试数据
1 w e t
2 s f 4
3 z c g
4 e a g
w a f w
6 k h d
t w f 2
[root@linuxprobe test]# sed '/^[0-9]/d' a.txt  ## 删除所有以数字开头的行
w a f w
t w f 2
[root@linuxprobe test]# sed '/^[a-zA-Z]/d' a.txt ## 删除所有以字母开头的行
1 w e t
2 s f 4
3 z c g
4 e a g
6 k h d
复制代码
复制代码
[root@linuxprobe test]# sed '/[0-9]$/d' a.txt ##删除所有以数字结尾的行
1 w e t
3 z c g
4 e a g
w a f w
6 k h d
[root@linuxprobe test]# sed '/[a-zA-Z]$/d' a.txt ##删除所有以字母结尾的行
2 s f 4
t w f 2
复制代码
复制代码
[root@linuxprobe test]# cat a.txt ## 使用vim 编辑器重新编辑测试数据
6 w e g
t s f g
2 z c g
2 e a t
y a f w
6 k h w
t w f 2
[root@linuxprobe test]# sed '/^2.*g$/d' a.txt  ## 删除以2开头,同时以g结尾的行
6 w e g
t s f g
2 e a t
y a f w
6 k h w
t w f 2
复制代码
复制代码

   [root@linuxprobe test]# cat a.txt ## 测试数据
   6 w e g
   t s f g
   2 z c g
   2 e a t
   y a f w
   6 k h w
   t w f 2

[root@linuxprobe test]# sed '/e/,+1d' a.txt ## 删除包含e及其后1行
2 z c g
6 k h w
t w f 2
[root@linuxprobe test]# sed '/z/,+2d' a.txt ## 删除包含z及其后2行
6 w e g
t s f g
6 k h w
t w f 2
复制代码

 

posted @   小鲨鱼2018  阅读(10470)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示