0

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.

1 Answer 1

5

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

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

13
  • @Sirex: Which option is specifying to search case-insensitively here? Commented Mar 31, 2011 at 10:37
  • 1
    the "I". Also if you have 102000 lines in one file maybe you need to refactor a bit. :) Commented Mar 31, 2011 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? Commented Mar 31, 2011 at 10:52
  • @Sirex: Meantime, I tried this command and it is working as I expected. Commented Mar 31, 2011 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 :) Commented Mar 31, 2011 at 11:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.