1

I'm assigning a variable as such:

cmd="sed -i 's/id="556"/id="33442233"\n    pss="120"/g' a.xml"

But when im trying to print cmd im getting a syntax error, seems it's because of multiple ' and "... how can I resolve this?

4 Answers 4

3

Escape the quotes using \ ?

cmd="sed -i 's/id=\"556\"/id=\"33442233\"\n pss=\"120\"/g' a.xml"
Sign up to request clarification or add additional context in comments.

Comments

2

Use triple quotes:

cmd = '''sed -i 's/id="556"/id="33442233"\n pss="120"/g' a.xml'''

Btw. I see \n there - if you want to keep it as backslash-n and not have it converted to a single newline character, you can even use this:

cmd = r'''sed -i 's/id="556"/id="33442233"\n pss="120"/g' a.xml'''

Comments

1

To escape quotes, put a backslash \ before the character. For example:
"\"hello\" said he." and '\'goodbye\' said she'

Comments

0

worked for me:

Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> cmd="sed -i \'s\/id=\"556\"\/id=\"33442233\"\n    pss=\"120\"\/g\' a.xml"
>>> print cmd
sed -i 's\/id="556"\/id="33442233"
    pss="120"\/g' a.xml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.