user1189851
user1189851

Reputation: 5041

awk commands within python script

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

Upvotes: 12

Views: 68266

Answers (2)

TehTris
TehTris

Reputation: 3217

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'

Upvotes: 13

Schorsch
Schorsch

Reputation: 7905

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.

Upvotes: 8

Related Questions