我必须在 python 脚本中使用 awk print。
我使用以下格式
a = commands.getoutput(" ls -l | awk \'{print $1, $2}\' | awk \'{if(NR>3)print}\'"
我收到以下错误:
KeyError: 'print $1, $2'
有人可以帮我解决一下吗?
最佳答案
您的 Awk 脚本可以简单地简化为单个脚本。无论如何,这里没有什么真正需要使用 Awk 的。
from subprocess import Popen, PIPE
with Popen(["ls", "-l"], stdout=PIPE, universal_newlines=True) as proc:
for index, line in enumerate(proc.stdout):
if index <= 2:
continue
print(' '.join(line.split()[0:2]))
关于python - 在 python 脚本中使用 awk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47468423/