python脚本中的awk命令 [英] awk commands within python script

查看:395
本文介绍了python脚本中的awk命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个python脚本,在其中需要调用一些awk命令.

I need to write a python script where I need to call a few awk commands inside of it.

#!/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($10,arr,"-")}{print arr[1]}'|sort|uniq -c")

第8行出现错误:SyntaxError:连续行字符后出现意外字符

It gives an error in line 8: SyntaxError: unexpected character after line continuation character

有没有一种方法可以让awk命令在python脚本中工作? 谢谢

Is there a way I can get the awk command get to work within the python script? Thanks

推荐答案

该字符串中有两种引号,因此请在整个内容中使用三引号

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($10,arr,"-")}{print arr[1]}'|sort|uniq -c'''
>>> x
'tail -n+2 ./*/*.tsv|cat|awk \'BEGIN{FS="\t"};{split($10,arr,"-")}{print arr[1]}\'|sort|uniq -c'

这篇关于python脚本中的awk命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆