sed 删除某一行_sed删除指定行

本文介绍了如何使用sed命令删除文本中的指定行,包括连续行和不连续行,并给出实际示例,例如删除list文件中与test文件对应大小为0的行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

待处理文本如1-8行文本

$cata1

2

3

4

5

6

7

8

sed删除连续指定行:如1-4行

$sed '1,4d'a5

6

7

8

sed删除不连续指定行:如第一行和第三行

$sed -e '1d' -e '3d'a2

4

5

6

7

8

一个例子:

我有一个list文件如下:

$cat list

unknown_other_2 #文件名,我要从文件里抓取一些信息,下同,发现下图里没有有些没有我想要的信息,test后面的数字代表这个list文件的行号

unknown_other_5   #我要删除大小为0的行

unknown_other_1

F002_other_1

unknown_other_14

t_unknown_other_4

t_unknown_other_6

t_unknown_other_4

t_unknown_other_1

t_unknown_other_4

t_unknown_other_12

t_unknown_other_4

t_unknown_other_13

t_unknown_other_10

t_unknown_other_12

t_unknown_other_4

t_unknown_other_42

t_unknown_other_16

t_unknown_other_27

t_unknown_other_14

t_unknown_other_31

t_unknown_other_12

t_unknown_other_10

t_unknown_other_13

t_unknown_other_11

t_unknown_other_22

t_unknown_other_21

t_unknown_other_45

t_unknown_other_42

t_unknown_other_4

t_unknown_other_33

t_unknown_other_1

t_unknown_other_12

t_unknown_other_14

t_unknown_other_1

t_unknown_other_31

t_unknown_other_4

t_unknown_other_11

t_unknown_other_11

t_unknown_other_3

test文件,test后面的数字对应上面list行号:

代码如下:

ls -l |awk '{if ($5==0){print $9}}'|cut -d "t" -f3|xargs -I {} echo -e "-e {}d"|xargs|xargs -I {} echo sed -i -e {} list|sh

#执行结果

#sed-i -e test10d -e test12d -e test14d -e test16d -e test2d -e test27d -e test3d -e test31d -e test32d -e test33d -e test35d -e test38d -e test39d -e test4d -e test40d -e test5d -e test9d list

#分解说明

#ls-l |awk '{if ($5==0){print $9}}'列出大小为0的test文件

#cut-d "t" -f3 抓取后面的数字 用字符"t"分割test文件,取第三列数字部分

#xargs-I {} echo -e "-e {}d"|xargs|xargs -I {} echo sed -i -e {} list 打印执行语句

#sh 执行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值