python脚本中的awk命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16675211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
awk commands within python script
提问by user1189851
I need to write a python script where I need to call a few awk commands inside of it.
我需要编写一个 python 脚本,我需要在其中调用一些 awk 命令。
#!/usr/bin/python
import os, sys
input_dir = '/home/abc/data'
os.chdir(input_dir)
#wd=os.getcwd()
#print wd
os.system ("tail -n+2 ./*/*.tsv|cat|awk 'BEGIN{FS="\t"};{split(,arr,"-")}{print arr[1]}'|sort|uniq -c")
It gives an error in line 8: SyntaxError: unexpected character after line continuation character
它在第 8 行给出错误:SyntaxError:行继续符后的意外字符
Is there a way I can get the awk command get to work within the python script? Thanks
有没有办法让 awk 命令在 python 脚本中工作?谢谢
采纳答案by TehTris
You have both types of quotes in that string, so use triple quotes around the whole thing
您在该字符串中有两种类型的引号,因此请在整个内容周围使用三重引号
>>> x = '''tail -n+2 ./*/*.tsv|cat|awk 'BEGIN{FS="\t"};{split(,arr,"-")}{print arr[1]}'|sort|uniq -c'''
>>> x
'tail -n+2 ./*/*.tsv|cat|awk \'BEGIN{FS="\t"};{split(,arr,"-")}{print arr[1]}\'|sort|uniq -c'
回答by Schorsch
You should use subprocessinstead of os.system:
您应该使用subprocess代替os.system:
import subprocess
COMMAND = "tail -n+2 ./*/*.tsv|cat|awk 'BEGIN{FS=\"\t\"};{split(,arr,\"-\")}{print arr[1]}'|sort|uniq -c"
subprocess.call(COMMAND, shell=True)
As TehTris has pointed out, the arrangement of quotes in the question breaks the command string into multiple strings. Pre-formatting the command and escaping the double-quotes fixes this.
正如 TehTris 所指出的,问题中引号的排列将命令字符串分成多个字符串。预先格式化命令并转义双引号可解决此问题。

