0

I have a basic python script that looks like this:

my_name = 'Zed'
my_age = 35
my_height = 74
my_weight = 180
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'

print "let's talk about %s" % my_name
print "he's %d inches tall" % my_height
print "he's %d pounds heavy" % my_weight
print "actually, that's not too heavy" 
print "he's got %s eyes and %s hair" % (my_eyes, my_hair)
print "his teeth are usually %s depending on the coffee" % my_teeth

print "if I add %d, %d, and %d, I'd get %d" %(my_age, my_height, my_weight, my_age + my_height + my_weight)

I wanted to delete all of the instances of "my_" in this file so I ran the sed command:

sed 's/my_//' pythonscript.py > altered_mypythonscript.py

The outputs deleted all of the instances of "my_" everywhere except for the line at the end of the script right here:

print "if I add %d, %d, and %d, I'd get %d" %(my_age, my_height, my_weight, my_age + my_height + my_weight)

Why is sed not replacing the words in the parenthesis I want it to, and how can I fix this? Maybe my google abilities have failed me, but I haven't read anything about sed's substitute command being affected by parenthesis. I could be wrong though.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

0

This has nothing to do with that being the last line, but with the fact that this subsitution

's/my_//'

only changes the first occurance on a line.

Change it to:

's/my_//g'

I would also consider, starting to use the print function (print()) by including

 from __future__ import print_function

at the top of your script or more general switch to using Python3 for these kind of scripts.

Anthon
  • 79,293