shell - How do I correctly pass double quotes to an awk subprocess in Python? -


i trying run simple awk shell command , capture output (using python2). here try do:

import subprocess sb  shell = ["awk '!/<tag>/ {print \"\\"\"$1\"\\"\", \"\\"\"$2\"\\"\"}' test.txt"] p = sb.check_output(shell, shell=true) print p 

test.txt content:

a, b, 5 a, c, 3 d, d, 1 

i want following output awk , store variable:

"a" "b" "a" "c" "d" "d" 

however lack knowledge of how handle double quotes. tried escaping them several backsplashes, didn't work. how correctly escape double quotes example above work?

when use shell=true pass list, you're asking python merge list of strings if separate arguments. means may own quoting, on top of whatever quoting did, in hopes shell reverse things properly. going nightmare right. if want use shell=true, pass string.

but raises question of why you're using shell=true in first place. if didn't use this, pass list of arguments, without having quote of them protect them shell. easier write, , easier debug, , more efficient , more secure boot. unless need shell features, or you've got command line worked hard working , don't want spend time breaking down separate arguments, never use shell.


i'm not sure awk command you're trying run here. if give double-quotes around $1 , $2 it's going print literal "$1" "$2", because that's quotes mean awk. maybe wanted this?

awk '!/<tag>/ {print "\""$1"\"", "\""$2"\""}' test.txt 

in case:

subprocess.check_output(['awk', r'!/<tag>/ {print "\""$1"\"", "\""$2"\""}',                           'test.txt']) 

(note used raw string pass "\"" literally, without having backslash backslash.)

but still doesn't provide desired output, because $1 going a,, "\""$1"\"" going "a,".


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -