sed : remove lines between two keywords [ including keywords ]

The post explains a useful sed command trick to remove all lines between two keywords. The lines which has these keywords will also be removed.

The below given command will show you the output on terminal and do not make any changes inside the file. So it is safe to use as preview output.

sed '/KEYWORD-1/','/KEYWORD-2/d' inputfile

In case you want to save the output in some file use below given syntax

sed '/KEYWORD-1/','/KEYWORD-2/d' inputfile > OutputFile.txt

To directly do the changes in input file.Use the -i switch

sed -i '/KEYWORD-1/','/KEYWORD-2/d' inputfile

Here we would like to show one more example by using pipe and grep command.A good example for linux newbie.

We are using 2 KEYWORDS in below examples –

 ls -l|sed '/orbit-sharad/','/skype-8904/d'

See the reference from our system

sed-command