linux 使用sed删除多行

13z8s7eq  于 2023-01-04  发布在  Linux
关注(0)|答案(3)|浏览(157)

我想删除模式匹配后的所有行,但不包括模式匹配行。
在以下示例中,删除匹配“1450/pm2-hmc 2”模式后出现的所有行,而不删除模式匹配行ex:

/root/abhishek/HPC/2023-01-03-1440/pm1-hmc1.txt
/root/abhishek/HPC/2023-01-03-1440/pm1-hmc2.txt
/root/abhishek/HPC/2023-01-03-1440/pm2-hmc0.txt
/root/abhishek/HPC/2023-01-03-1440/pm2-hmc1.txt
/root/abhishek/HPC/2023-01-03-1440/pm2-hmc2.txt
/root/abhishek/HPC/2023-01-03-1445/pm0-hmc0.txt
/root/abhishek/HPC/2023-01-03-1445/pm0-hmc1.txt
/root/abhishek/HPC/2023-01-03-1445/pm0-hmc2.txt
/root/abhishek/HPC/2023-01-03-1445/pm1-hmc0.txt
/root/abhishek/HPC/2023-01-03-1445/pm1-hmc1.txt
/root/abhishek/HPC/2023-01-03-1445/pm1-hmc2.txt
/root/abhishek/HPC/2023-01-03-1445/pm2-hmc0.txt
/root/abhishek/HPC/2023-01-03-1445/pm2-hmc1.txt
/root/abhishek/HPC/2023-01-03-1445/pm2-hmc2.txt
/root/abhishek/HPC/2023-01-03-1450/pm0-hmc0.txt
/root/abhishek/HPC/2023-01-03-1450/pm0-hmc1.txt
/root/abhishek/HPC/2023-01-03-1450/pm0-hmc2.txt
/root/abhishek/HPC/2023-01-03-1450/pm1-hmc0.txt
/root/abhishek/HPC/2023-01-03-1450/pm1-hmc1.txt
/root/abhishek/HPC/2023-01-03-1450/pm1-hmc2.txt
/root/abhishek/HPC/2023-01-03-1450/pm2-hmc0.txt
/root/abhishek/HPC/2023-01-03-1450/pm2-hmc1.txt
/root/abhishek/HPC/2023-01-03-1450/pm2-hmc2.txt
/root/abhishek/HPC/2023-01-03-1455/pm0-hmc0.txt
/root/abhishek/HPC/2023-01-03-1455/pm0-hmc1.txt
/root/abhishek/HPC/2023-01-03-1455/pm0-hmc2.txt
/root/abhishek/HPC/2023-01-03-1455/pm1-hmc0.txt
/root/abhishek/HPC/2023-01-03-1455/pm1-hmc1.txt
/root/abhishek/HPC/2023-01-03-1455/pm1-hmc2.txt
/root/abhishek/HPC/2023-01-03-1455/pm2-hmc0.txt
/root/abhishek/HPC/2023-01-03-1455/pm2-hmc1.txt
/root/abhishek/HPC/2023-01-03-1455/pm2-hmc2.txt
/root/abhishek/HPC/2023-01-03-1500/pm0-hmc0.txt
/root/abhishek/HPC/2023-01-03-1500/pm0-hmc1.txt
/root/abhishek/HPC/2023-01-03-1500/pm0-hmc2.txt
/root/abhishek/HPC/2023-01-03-1500/pm1-hmc0.txt
/root/abhishek/HPC/2023-01-03-1500/pm1-hmc1.txt
/root/abhishek/HPC/2023-01-03-1500/pm1-hmc2.txt
/root/abhishek/HPC/2023-01-03-1500/pm2-hmc0.txt
/root/abhishek/HPC/2023-01-03-1500/pm2-hmc1.txt
/root/abhishek/HPC/2023-01-03-1500/pm2-hmc2.txt

我尝试用下面的命令,但它是删除匹配的模式行也.

sed '/1450\/pm2-hmc2/,$d' input.txt
mkshixfv

mkshixfv1#

您可以反转条件,即打印从开始到匹配行的所有内容:

sed '1,/1450\/pm2-hmc2/!d'
hgb9j2n6

hgb9j2n62#

sed -i '/1450\/pm2-hmc2/q' input.txt
lpwwtiir

lpwwtiir3#

这应该能解决你的问题

sed '/1450\/pm2-hmc2/d' input_b.txt > output.txt

相关问题