Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk commands within python script

Tags:

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")

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

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

like image 605
user1189851 Avatar asked May 21 '13 16:05

user1189851


1 Answers

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'
like image 175
TehTris Avatar answered Oct 04 '22 15:10

TehTris