Sample Header Ad - 728x90

Prepend two lines containing comments and URLs before specific line in python-scripts using sed

0 votes
1 answer
81 views
In some of my python-scripts, I've put the line if __name__ == '__main__' in order to make the content only be executed, when the script is run itself as main-script (as opposed to being imported, see this explanation ). As I've found that out just recently, I wanted to add two informative comment-lines before that line using sed. *The lines I want to prepend are:* # Docs on scope of __name__ == "__main__": https://stackoverflow.com/a/419185/12298276 # -> Makes boolean "execute_in_script = True/False" obsolete In order to accomplish this, I put up the following find - sed - command which shall make the desired change in all my python-scripts automatically: find /home/andylu/Desktop/Python/Scripts/ -not -path "*.history*" -not -path '*__pycache__*' -type f -exec sed -i -Ee '/^if __name__ == '__main__':.*/i # Docs on scope of __name__ == "__main__": https://stackoverflow.com/a/419185/12298276\n# -> Makes boolean "execute_in_script = True/False" obsolete' {} \; Unfortunately, it doesn't work. It seems to be running as it takes like 30 seconds to complete, but there is neither any output in the console, nor changes in the python-scripts. How do I have to correct my sed-string, which is like /line of interest/i Lines to\nprepend before that line, to make it work? ---------- Documentation on how to prepend or append lines with *sed*: See this blog post .
Asked by Andreas L. (305 rep)
Dec 22, 2020, 08:55 PM
Last activity: Dec 24, 2020, 06:49 PM