0

For example: old file

def MyFunction():

       some code inside MyFunction()

def MyFunction1():

       some code inside MyFunction1()

if __name__ == "__main__":

       some code inside

New file

def MyFunction1():

       some code inside MyFunction1()

if __name__ == "__main__":

       some code inside

I want to remove MyFunction() and code inside it from multiple files and MyFunction() is not followed by MyFunction1() . MyFunction() Could be located any where in the code followed by any code.

akshay
  • 1
  • 1
  • hi Sundeep there are 2 function MyFunction() and MyFunction1() and following by some indented code i want to remove MyFunction and the indented code in it from my file.And keep Myfunction1() and the remaning indented code in it – akshay Feb 27 '18 at 08:47
  • please click https://unix.stackexchange.com/posts/426865/edit and add those details.. see https://unix.stackexchange.com/editing-help for formatting help... also add what you've tried.. – Sundeep Feb 27 '18 at 08:53
  • The line immediately after the `def` line does not seem to be indented. Do you want to remove empty lines too? – Kusalananda Feb 27 '18 at 09:55

1 Answers1

0
awk '/^[[:alpha:]]/         { del=0 }
     /^def MyFunction\(\):/ { del=1 }
     del == 0' file.py

This awk program will pass through everything as long as the variable del is set to zero.

The variable gets set to 1 if the first line of the definition of MyFunction() is found, and then reset to zero when a line with an alphabetic character in the beginning of the line is found.

This deletes the MyFunction() definition from the input file.

The result when running this on the example data is

def MyFunction1():

       some code inside MyFunction1()

if __name__ == "__main__":

       some code inside
Kusalananda
  • 14,885
  • 3
  • 41
  • 52