Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed one liner not working in python subprocess

I am trying to incorporate this sed command to remove the last comma in a son file.

sed -i -e '1h;1!H;$!d;${s/.*//;x};s/\(.*\),/\1 /' file.json"

when i run this in the command line, it works fine. When i try to run as a subprocess as so it doesn't work.

   Popen("sed -e '1h;1!H;$!d;${s/.*//;x};s/\(.*\),/\1 /' file.json",shell=True).wait()

What am I doing wrong?

like image 675
Ahk86 Avatar asked Oct 17 '22 14:10

Ahk86


1 Answers

It doesn't work because when you write \1, python interprets that as \x01 and our regular expression doesn't work / is illegal.

That is already better:

check_call(["sed","-i","-e",r"1h;1!H;$!d;${s/.*//;x};s/\(.*\),/\1 /","file.json"])

because splitting as a real list and passing your regex as a raw string has a better chance to work. And check_call is what you need to just call a process, without caring about its output.

But I would do even better: since python is good at processing files, given your rather simple problem, I would create a fully portable version, no need for sed:

# read the file
with open("file.json") as f:
   contents = f.read().rstrip().rstrip(",")  # strip last newline/space then strip last comma
# write back the file
with open("file.json","w") as f:
   f.write(contents)
like image 62
Jean-François Fabre Avatar answered Oct 21 '22 08:10

Jean-François Fabre