Awk in python script

I need to write a python script where I need to call several awk commands inside 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 on line 8: Syntax Error: unexpected character after line continuation character

Is there any way to get awk command to work in python script? Thanks

+4
source share
2 answers

You have both types of quotes in this line, so use triple quotes around everything

 >>> 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' 
+7
source

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 pointed out, the location of the quotation marks in the question splits the command line into several lines. Pre-formatting the command and escaping double quotes corrects this.

+6
source

Source: https://habr.com/ru/post/1481999/


All Articles