JohnJ JohnJ - 25 days ago 10
Linux Question

copy file (using bash) as new file and edit variables within the file

I have a

python
file (
.py
) which I am trying to copy via bash, which is quite strightforward like so:

cp filename.py filename1.py


However, what I want to do is change a few variables when this copy happens. So, the contents of the filename.py are like so:

....
DEBUG = True
def new()
var=3
return False
other contents..


when the copy happens, I want `filename1.py to have:

....
DEBUG = True
def new()
var=4
return False
other contents..


How can achieve this in one line using bash? So, in the new file, the want the variables
DEBUG
and
var
to have different values.

I am assuming it will be a combination of
grep
and
cat
but I am not able to think how to use these to achieve the desired result.

Any help would be great.

Answer

Have you tried this?

cat infile.py | sed 's/\DEBUG\ \=\ True/DEBUG\ \=\ False/g' > outfile.py