1

I am using Python to do sed delete / replace with the help of the subprocess. Somehow I am not getting the number of escapes correct. Here is my code:

from subprocess import call

remover = ["sed", "-i", "'1d'", "file"]
call(remover)

removeq = ["sed", "-i", "'s/\"//g'", "file"]
call(removeq)

Both of these tasks produce the error message:

sed: -e expression #1, char 1: unknown command: `''

How many times does the ' sign need to be escaped in the first list and how many times does the " have to be escaped in the last one? I tried once, twice and three times, but to no avail. There are also no hidden characters that can potentially mess something up.

Any suggestions?

EKarl
  • 149
  • 1
  • 11
  • The single quotes are incorrect. They are used when running the command at the shell to quote the word but when executed directly (no shell involved) they are treated as literal characters and confuse `sed` (as can be seen). – Etan Reisner May 04 '15 at 14:39
  • possible duplicate of [subprocess call of sed command giving error](http://stackoverflow.com/questions/25522639/subprocess-call-of-sed-command-giving-error) – tripleee May 04 '15 at 15:20
  • 2
    "I am using Python to do sed delete / replace with ..." Why? Just do it natively in Python... – twalberg May 04 '15 at 16:06
  • As I understand it, it is simpler to do it with sed if you want it done in place without creating a new file? – EKarl May 04 '15 at 19:18
  • Possibly, if you define "simple" as "fewest lines of actual code, but less efficient because it spawns extra processes, with the associated fork(), exec(), potential disk accesses and other overhead..." – twalberg May 04 '15 at 19:29
  • As I understood it, most Python methods involve generating a new output file? Such as reading in line-by-line, skipping the first and printing the rest into another file? – EKarl May 04 '15 at 21:22
  • So does `sed -i`. It just does the bookkeeping for you. – twalberg May 04 '15 at 23:00
  • Alright, that was new information for me. Might try Python then. Thanks. – EKarl May 05 '15 at 09:37
  • You could hide [the bookkeeping](http://stackoverflow.com/a/9834294/4279) in Python too. See [Is it possible to modify lines in a file in-place?](http://stackoverflow.com/q/5453267/4279) – jfs May 09 '15 at 18:02

1 Answers1

4

Fix the quoting mechanism:

sed -i 's/\"//g' file

Should be just:

sed -i 's/"//g' file

You can also take adventage of shlex library. Example from interpreter:

>>> import  shlex
>>> cmd = "sed -i '1d' file"
>>> shlex.split(cmd)
['sed', '-i', '1d', 'file']
>>> cmd = """sed -i 's/"//g' file"""
>>> shlex.split(cmd)
['sed', '-i', 's/"//g', 'file']
Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52
  • `shlex` is not very sophisticated. [It may break easily: `r'echo "\$x"'`](http://stackoverflow.com/questions/28468807/python-executing-a-shell-command#comment45287698_28468860) – jfs May 09 '15 at 18:00