awk commands within python script

前端 未结 2 1351
闹比i
闹比i 2021-01-02 11:21

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.ch         


        
相关标签:
2条回答
  • 2021-01-02 12:14

    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'
    
    0 讨论(0)
  • 2021-01-02 12:23

    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.

    0 讨论(0)
提交回复
热议问题