9

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

user1189851
  • 4,581
  • 14
  • 46
  • 67

2 Answers2

11

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'
Aya
  • 36,657
  • 6
  • 48
  • 54
TehTris
  • 3,059
  • 1
  • 20
  • 33
  • 1
    stack overflow doesn't format it correctly in the `code` blocks, but if you copy paste the line into the interpreter you'll see what I mean. – TehTris May 21 '13 at 16:52
8

You should use subprocess instead of os.system:

import subprocess
COMMAND = "tail -n+2 ./*/*.tsv|cat|awk 'BEGIN{FS=\"\t\"};{split($10,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.

Schorsch
  • 7,511
  • 6
  • 36
  • 64