Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Am looking for a help in Linux command that performs/does the following:

Searches for a particular word/phrase case-insensitively in a given file, and then remove/delete the immediate next 'n' lines including the line where the word/phrase was matched in the given file.

EXAMPLE: If I try to search for the phrase "CREATE FUNCTION plpgsql_call_handler" case-insensitively and if it matches at line no.102644, then I expect to delete the line no.102644 and the immediate next 2 lines from the given file. In this case, I expect to delete line nos. 102644,102645,102646.

share|improve this question
up vote 5 down vote accepted

sed -i '/CREATE FUNCTION plpgsql_call_handler/I ,+2d' <filename>

Believe this'll work. Back the file up first before trying it in anger.

share|improve this answer
@Sirex: Which option is specifying to search case-insensitively here? – Gnanam Mar 31 '11 at 10:37
the "I". Also if you have 102000 lines in one file maybe you need to refactor a bit. :) – Sirex Mar 31 '11 at 10:42
@Sirex: So did you mean to say this command either fails or becomes slow, if the total no. of lines in the file exceeds 102000? – Gnanam Mar 31 '11 at 10:52
@Sirex: Meantime, I tried this command and it is working as I expected. – Gnanam Mar 31 '11 at 10:56
No it should fun fine, it's just having files with 100,000 lines in them is well on the road to madness :) – Sirex Mar 31 '11 at 11:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.