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.

link|improve this question

68% accept rate
feedback

1 Answer

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.

link|improve this answer
@Sirex: Which option is specifying to search case-insensitively here? – Gnanam Mar 31 '11 at 10:37
1  
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
show 8 more comments
feedback

Your Answer

 
or
required, but never shown

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