-2

My required output is :

 'grep "key\":\"value" test_File.txt'

for example, what i tried is..

>>> cmd = 'grep "key\":\"value" test_File.txt'
>>> cmd
 'grep "key":"value" test_File.txt'

but it prints two slashes when i try to escape the actual slash.

>>> cmd = 'grep "key\\":\\"value" test_File.txt'
>>> cmd
 'grep "key\\":\\"value" test_File.txt'
 >>> 

all i need is ,how can i get the first line as output ?

chetan honnavile
  • 398
  • 1
  • 6
  • 19
  • 2
    just `print(cmd)`. You are printing the `repr` of the string. – juanpa.arrivillaga Mar 16 '17 at 06:02
  • What are you actually trying to accomplish here? This looks very much like you are trying to print a command to be executed by the shell, and being very confused about the shell's quoting rules. A valid `grep` command would look like `grep '"key":"value"' test_File.txt` but I am second-guessing several details here. With `subprocess` you want to avoid `shell=True` and pass just `['grep', '"key":"value"', 'test_File.txt']` instead of a string. – tripleee Mar 16 '17 at 06:15
  • yeah,i am trying to pass this as a command to subprocess to get executed , – chetan honnavile Mar 16 '17 at 06:25
  • This isn't a real duplicate of anything, but instead a typo. OP wanted to `print` something, but wrote code that doesn't use `print`; there is nothing wrong with the actual string contents. – Karl Knechtel Aug 07 '22 at 01:55

1 Answers1

0

Don't get the escaped string confused with what's actually printed.

>>> cmd = '\'grep "key\\":\\"value\" test_File.txt\''
>>> print cmd
'grep "key\":\"value" test_File.txt'

I believe that matches your required output.

selbie
  • 100,020
  • 15
  • 103
  • 173