在 Python 中使用 SubProcess for Awk
Using SubProcess in Python for Awk
我正在尝试从 Python 文件中 运行 此命令 git status -vv | awk 'NR>5 {print [=14=]}'
。但是我无法让 awk 命令工作。
这是我的 git st:
的示例结果
# On branch master
# Your branch is ahead of master by 2 commits.
#
#
# modified: file1
# modified: file2
# modified: file3
当我 运行 来自终端的命令时,我得到了我想要的:
# modified: file1
# modified: file2
# modified: file3
我在 Python 脚本中实现它时遇到问题:
import sys
import subprocess as sb
ps = sb.Popen(("git","status","-vv"),stdout=sb.PIPE)
output = sb.check_output(('awk','"NR>5 {print [=13=]}"'),stdin=ps.stdout)
print output
然而,这 returns 只是 git st 结果而没有在线执行 awk。我如何从 python 中执行此操作以获得与在终端中 运行ning 时相同的输出。
这可能要简单得多:
#!/usr/bin/python3
import sys
import subprocess as sb
cmd = "git status -vv | awk '(NR>5){ print [=10=] }'"
output = sb.check_output(cmd, stderr=sb.STDOUT, shell=True)
sys.stdout.write('{}'.format(output))
下面的代码应该可以工作(只需删除 awk 参数的双引号)
import sys
import subprocess as sb
ps = sb.Popen(("git","status","-vv"),stdout=sb.PIPE)
output = sb.check_output(('awk','NR>5 {print [=10=]}'),stdin=ps.stdout)
print output
我正在尝试从 Python 文件中 运行 此命令 git status -vv | awk 'NR>5 {print [=14=]}'
。但是我无法让 awk 命令工作。
这是我的 git st:
的示例结果# On branch master
# Your branch is ahead of master by 2 commits.
#
#
# modified: file1
# modified: file2
# modified: file3
当我 运行 来自终端的命令时,我得到了我想要的:
# modified: file1
# modified: file2
# modified: file3
我在 Python 脚本中实现它时遇到问题:
import sys
import subprocess as sb
ps = sb.Popen(("git","status","-vv"),stdout=sb.PIPE)
output = sb.check_output(('awk','"NR>5 {print [=13=]}"'),stdin=ps.stdout)
print output
然而,这 returns 只是 git st 结果而没有在线执行 awk。我如何从 python 中执行此操作以获得与在终端中 运行ning 时相同的输出。
这可能要简单得多:
#!/usr/bin/python3
import sys
import subprocess as sb
cmd = "git status -vv | awk '(NR>5){ print [=10=] }'"
output = sb.check_output(cmd, stderr=sb.STDOUT, shell=True)
sys.stdout.write('{}'.format(output))
下面的代码应该可以工作(只需删除 awk 参数的双引号)
import sys
import subprocess as sb
ps = sb.Popen(("git","status","-vv"),stdout=sb.PIPE)
output = sb.check_output(('awk','NR>5 {print [=10=]}'),stdin=ps.stdout)
print output