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.

Gnanam
  • 1,459
  • 13
  • 26
  • 32

1 Answers1

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.

Sirex
  • 5,499
  • 2
  • 33
  • 54
  • @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
  • @Sirex: Actually, it's a database SQL dump file and that's the reason to have so much lines in the file. Hope this makes you clear. You also said `...you need to refactor a bit.` - how do I refactor in that case. In fact I'm not an expert in shell scripting, etc. Hope you can help me in refactoring. – Gnanam Mar 31 '11 at 11:33
  • not to worry then ! My fault for not reading question properly, thought it was code :) – Sirex Mar 31 '11 at 12:36
  • As a side note, maybe this'll help make them a bit more managable: http://stackoverflow.com/questions/1301147/restoring-mysql-db-from-the-contents-of-split-up-mysqldump – Sirex Mar 31 '11 at 12:39
  • @Sirex: This is just out of my curiosity. What is the equivalent syntax/command for the same using `awk`? – Gnanam Apr 01 '11 at 05:13
  • `awk '/CREATE FUNCTION plpgsql_call_handler/{a=3}!(a&&a--)' ` – Sirex Apr 01 '11 at 14:52
  • @Sirex: Thanks for providing the equivalent `awk` command. I tried out this command, it is not making the changes in the specified file. Instead, output is shown in the console. What is the option, that would make the changes in the file specified itself? In case of `sed`, -i is the option that made the changes *in-place*. – Gnanam Apr 05 '11 at 07:51
  • yes, i found that also. From my searching of google it seems doing it in place with awk is a pain, i saw a few "tricks" to do it but nothing that was neat and tidy. Perhaps just pipe the output to anouther file using > and copy it back over the input file. – Sirex Apr 05 '11 at 13:07
  • @Sirex: Thanks a lot for your patience and effort in finding an answer. Would you also be kind enough to explain what exactly `{a=3}!(a&&a--)` does in *awk* command? Am not able to understand this as a whole. Thanks again. – Gnanam Apr 05 '11 at 13:28