此代码在终端/ bash脚本中正常工作:
awk '/^(name|count|region)/{sep = (/^region/? ORS:OFS);gsub(/^[^:]+:[[:space:]]*|[[:space:]]*$/, "");printf "%s%s", $0, sep}' output.txt>1.txt
尝试从python中执行它:
#!/usr/bin/python
import sys
import json
import re
import os
import subprocess
def bash_command(cmd):
subprocess.Popen(cmd, shell=True, executable='/bin/bash')
bash_command = ('''"awk '/^(name|count|region)/{sep = (/^region/? ORS:OFS);gsub(/^[^:]+:[[:space:]]*|[[:space:]]*$/, "");printf "%s%s", $0, sep}' output.txt> 1.txt"''')
没有错误但是没有创建1.txt
答案 0 :(得分:1)
要使用字符串参数调用函数,必须使用此伪代码。相反,您将函数名称设置为字符串元组。
#!/usr/bin/python
import sys
import json
import re
import os
import subprocess
def bash_command(cmd):
subprocess.Popen(cmd, shell=True, executable='/bin/bash')
bash_command("echo test")
答案 1 :(得分:0)