0

I have the following python file

#!/usr/bin/env python
import sys, argparse
from pbsuite.utils.setupLogging import *
....

and I would like to change it to:

#!/usr/bin/env python
sys.path.append('/anaconda/conda-bld/pbsuite_1490823990330/_t_env/opt/pbsuite-15.8.24')
import sys, argparse
from pbsuite.utils.setupLogging import *

Unfotunately, sed -i.bak '2 s|^.*$|sys.path.append(${OUTPUT})|g' a.py overwrites import sys, argparse and does not replace $OUTPUT variable with its content

#!/usr/bin/env python
sys.path.append(${OUTPUT})
from pbsuite.utils.setupLogging import *

How is it possible to fix it for Mac and Linux?

2
$ sed "2i\sys.path.append(\'/anaconda/conda-bld/pbsuite_1490823990330/_t_env/opt/pbsuite-15.8.24\')" file.py
#!/usr/bin/env python
sys.path.append('/anaconda/conda-bld/pbsuite_1490823990330/_t_env/opt/pbsuite-15.8.24')
import sys, argparse
from pbsuite.utils.setupLogging import *
....

awk solution

$ a="sys.path.append('/anaconda/conda-bld/pbsuite_1490823990330/_t_env/opt/pbsuite-15.8.24')"

$ echo $a
sys.path.append('/anaconda/conda-bld/pbsuite_1490823990330/_t_env/opt/pbsuite-15.8.24')

$ awk -v ins="${a}" 'NR==2{printf("%s\n",ins)}1' a.txt
#!/usr/bin/env python
sys.path.append('/anaconda/conda-bld/pbsuite_1490823990330/_t_env/opt/pbsuite-15.8.24')
import sys, argparse
from pbsuite.utils.setupLogging import *
....
  • Under Mac I got this error sed: 1: "2i\sys.path.append(\'/a ...": extra characters after \ at the end of i command. What did I do wrong? – user977828 Mar 30 '17 at 2:02
  • can you check this stackoverflow.com/a/21950403 – Kamaraj Mar 30 '17 at 3:09
  • yes, the mac solution work for mac, but it does not work under linux. Is there any platform independent solution? – user977828 Mar 30 '17 at 3:48
  • check.. whether awk works for you in linux and mac... i am sure.. the sed will work on linux and mac.. – Kamaraj Mar 30 '17 at 5:02
1
sed -i.bak -e "1G;s|\n|&sys.path.append('/anaconda/conda-bld/pbsuite_1490823990330/_t_env/opt/pbsuite-15.8.24')|" yourpyfile

ed -s yourpyfile <<\cmd
1a
sys.path.append('/anaconda/conda-bld/pbsuite_1490823990330/_t_env/opt/pbsuite-15.8.24')
.
wq
cmd
0

This is more a shell quoting issue; the single quotes you use make the ${OUTPUT} literal:

$ export OUTPUT="a test"
$ echo 'bla blah ${OUTPUT} blah'
bla blah ${OUTPUT} blah
$ echo 'blah blah '"${OUTPUT}"' blah'                   
blah blah a test blah
$ 

Note that 'blah blah '${OUTPUT}' blah' may appear correct though has subtle differences from the above version with the double quotes.

0

as per your condition, if you want to add one line between those two lines then first you need to change single quote to double quote , then only sed will understand that you have shell variable and use something like this,

Note that space between 1 and a\sys.path.append(${OUTPUT}) is important.

sed -i.bak "1 a\sys.path.append(${OUTPUT})" a.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.