-3

As you probably guessed, I'm using a package that was built using Python 2 on Python 3, and I need to make some changes to get it to work. I asked around and someone suggested that sed is the best option. Though I'm not exactly sure how to use it. Can anyone help out? Thanks in advance.

I'm wanting to replace all instances of print 'example' with print('example')

maj
  • 79
  • 1
  • 9
  • Does this answer your question? [How to use 2to3 properly for python?](https://stackoverflow.com/questions/20458011/how-to-use-2to3-properly-for-python) – dspencer Mar 10 '20 at 08:45

2 Answers2

0
sed "s#'.*'#(&)#g"

explanation:

sed "  <-- Use double quote for marking start and end 
s# <-- # is delimter
'.*' <--  Regex pattern. Start with ' followed by any char till '
#(&)#g" <-- Matched pattern is stored in & 

Demo:

:>echo "print 'example'" | sed "s#'.*'#(&)#g"
print ('example')
:>

Digvijay S
  • 2,665
  • 1
  • 9
  • 21
0

I think that we have so-called XY problem here. You can use sed to change print statements (as used in Python 2) into print functions (as used in Python 3), however there are other differences between Python 2 and Python 3, so even if you manage change prints your code might not work as intended. I suggest you trying 2to3 conversion tool.

Daweo
  • 31,313
  • 3
  • 12
  • 25