Home > other >  Delete multiple lines using sed
Delete multiple lines using sed

Time:01-03

I want to delete all lines after pattern match and excluding the pattern match line.

here in below example delete all the lines which occurs after matching "1450/pm2-hmc2" pattern found withou t deleting pattern match line 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

I tried with below command, but it is deleting the matching pattern line also.

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

CodePudding user response:

You can just invert the condition, i.e. print everything from the start up to the matching line:

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

CodePudding user response:

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

CodePudding user response:

This should solve your problem,

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

enter image description here

  • Related